"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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With