M-W*_*eEh 10 animation android android-custom-view android-animation
我有一个简单的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="animate"
android:text="animate" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
并在我的活动中,我在更改其y
位置后打印按钮的矩形:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void animate(View view) {
printHitRect();
findViewById(R.id.button1).setY(50);
printHitRect();
}
private void printHitRect() {
Rect rect = new Rect();
findViewById(R.id.button1).getHitRect(rect);
Log.d(">>button1 hit rect", rect.flattenToString());
}
}
Run Code Online (Sandbox Code Playgroud)
预期输出
button1 hit rect:0 0 116 72
button1 hit rect:0 50 116 122
实际输出
button1 hit rect:0 0 116 72
button1命中rect:-58 14 58 86
有人可以解释这个输出,我做错了什么或是一个错误?基本上我getHitRect()
在我的自定义中使用它ViewGroup
来检测哪个子用户已经触摸过.是否有更好的方法让孩子在特定的地方,可能是一个像这样的功能getChildAt(x, y)
?
而不是setY()
,我试过setTranslateY()
.我也使用过NineOldAndroid库以及内置的动画框架.如果我使用findViewById(R.id.button1).animate().y(50)
而不是,可以看到相同的行为setY()
.
更新:
我最后使用现在正在运行的nineoldandroid库编写实用程序方法:
private static void getHitRect(View v, Rect rect) {
rect.left = (int) com.nineoldandroids.view.ViewHelper.getX(v);
rect.top = (int) com.nineoldandroids.view.ViewHelper.getY(v);
rect.right = rect.left + v.getWidth();
rect.bottom = rect.top + v.getHeight();
}
Run Code Online (Sandbox Code Playgroud)
getHitRect()
有一个错误,不会正确应用转换.我们在内部修复了此错误,修复程序将在Android的下一个公开发行版中提供.
归档时间: |
|
查看次数: |
5659 次 |
最近记录: |