GethitRect (), I am doing it wrong, how does it work?

I keep getting top, bottom, left, right = 0. I think I'm doing it wrong, what's right? TIA

in onCreate()

    ImageView trash = (ImageView) findViewById(R.id.dropTarget_trash);
    trash.setOnTouchListener(this);
    Rect trashHit = new Rect();
    trash.getHitRect(trashHit);
    Log.d(TAG,"Trash left:" + trashHit.left + " right: " + trashHit.right + "  top: " + trashHit.top + " bottom: " + trashHit.bottom);
+3
source share
1 answer

Since the straight line rect is in the parent coordinate space, the parent must first arrange its children, which it has not yet executed in time onCreate(). Have a look at the solution here for an example of working in post(). If you have a custom view, the getHitRect()inside onDraw()will also give you the correct dimensions.

+2
source

Source: https://habr.com/ru/post/1769549/


All Articles