Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

image

I keep getting top, bottom, left, right = 0. I guess I am doing this wrong, what is the correct way? 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);
like image 346
Jim Avatar asked Oct 29 '25 20:10

Jim


1 Answers

As the hit rect is in the parent coordinate space, the parent first needs to layout its children which it hasn't done yet during onCreate(). Take a look at the solution here for an example of running in post(). If you have a custom view, getHitRect() within onDraw() will give you the correct dimensions too.

like image 175
dchang Avatar answered Oct 31 '25 11:10

dchang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!