Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the definition of the value supplied by the Android function View.getHitRect()?

Tags:

android

"Hit rectangle in parent's coordinates". But what does that mean?

To amplify, what I really want to know is the meaning of the phrase "hit rectangle". What is it for? How does the framework process it? When in the lifecycle is the return value meaningful? How might it differ from the rectangle defined by getLeft(),getTop(), getRight(), getBottom()?

Based on the name of the function I can of course guess at an answer, and try a few examples, but that's not satisfactory. I can find nothing useful about this function on the Android Developer website, or anywhere else I've looked.

like image 873
JulianSymes Avatar asked Nov 08 '12 19:11

JulianSymes


2 Answers

Here appears to be the most complete explanation.

The getHitRect() method gets the child's hit rectangle (touchable area) in the parent's coordinates.

The example snippet uses the function to determine the current touchable area of a child view (after layout) in order to effectively extend it by creating a TouchDelegate

like image 170
JulianSymes Avatar answered Sep 30 '22 04:09

JulianSymes


They should certainly do a better job of documenting. If you look at the source View#getHitRect(Rect), you'll see that if the view has an "identity matrix", or is not attached to a window, it returns exactly what we're thinking. The alternate branch means the view has a transform, therefore to get the 'hit rect' for the parent, which is the smallest possible rect in its coordinate system that covers view, you have to move the rect to origin, run the transform and then add back its original position.

So you can use it as a shortcut for this purpose if there's no transform. If there's a transform, remember that you'll be getting values in the rect that may be outside or inside the view as currently displayed.

like image 34
androidguy Avatar answered Sep 30 '22 04:09

androidguy