奇怪的View.getHitRect()行为奇怪、行为、View、getHitRect

2023-09-06 02:40:26 作者:予囚

我有一个简单的布局:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

<按钮
    机器人:ID =@ + ID /按钮1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文本=按钮/>

<按钮
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:的onClick =动画
    机器人:文本=动画/>

< / LinearLayout中>
 

和我的活动我打印按钮命中矩形改变其Ÿ后位置:

 公共类MainActivity延伸活动{

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
}

公共无效动画(查看视图){
    printHitRect();
    findViewById(R.id.button1).setY(50);
    printHitRect();
}
私人无效printHitRect(){
    矩形RECT =新的矩形();
    findViewById(R.id.button1).getHitRect(RECT);
    Log.d(>> button1的击中矩形,rect.flattenToString());
}
}
 

期望的输出

              

按钮1命中RECT:0 0 116 72

             

按钮1命中RECT:0 50 116 122

       

实际产量

              

按钮1命中RECT:0 0 116 72

             

按钮1命中RECT:-58 14 58 86

       

有人能解释这个输出,我是不是做错了什么或者是一个错误?基本上我用我的自定义的ViewGroup getHitRect()来检测其子用户触摸。有没有更好的办法让孩子在一个特定的点,可能会像函数getChildAt(X,Y​​)

而不是 SETY的(),我已经试过 setTranslateY()。我也用NineOldAndroid库以及内置的动画框架。同样的行为,如果我使用中可以看出 findViewById(R.id.button1).animate()。Y(50)而不是塞蒂()

更新:

我结束了编写实用方法使用nineoldandroid库,是现在的工作:

 私有静态无效getHitRect(视图V,矩形RECT){
    rect.left =(int)的com.nineoldandroids.view.ViewHelper.getX(ⅴ);
    rect.top =(int)的com.nineoldandroids.view.ViewHelper.getY(ⅴ);
    rect.right = rect.left + v.getWidth();
    rect.bottom = rect.top + v.getHeight();
}
 
如何构建 Android MVVM 应用程序

解决方案

getHitRect()有一个bug,并且不会应用转换正常。我们在公司内部解决了这个错误,并在Android的下一个公开发布的修补程序将可用。

I have a simple layout:

<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>

and in my Activity I am printing hit rect of button after changing its ylocation:

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()); 
} 
}

EXPECTED OUTPUT

button1 hit rect: 0 0 116 72

button1 hit rect: 0 50 116 122

ACTUAL OUTPUT

button1 hit rect: 0 0 116 72

button1 hit rect: -58 14 58 86

Can someone explain this output, am I doing something wrong or is it a bug? Basically I am using this getHitRect() in my custom ViewGroup to detect which child user has touched. Is there a better way to get the child at a particular point, may be a function like getChildAt(x, y)?

Instead of setY(), I have tried setTranslateY(). I also have used NineOldAndroid library as well as built in animation framework. Same behaviour can be seen if I use findViewById(R.id.button1).animate().y(50) instead of setY().

UPDATE:

I ended up writing utility method using nineoldandroid library that is working now:

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();
}

解决方案

getHitRect() had a bug and would not apply transforms properly. We fixed this bug internally and the fix will be made available in the next public release of Android.