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

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

To amplify, I really want to know the meaning of the phrase “hit rectangle”. What is this for? How is it processed? When does the return value make sense in the life cycle? How can it differ from the rectangle defined by getLeft() , getTop() , getRight() , getBottom() ?

Based on the name of the function, I can of course guess the answer and try a few examples, but this is unsatisfactory. I can not find anything useful in this feature on the Android developer website or in another place where I looked.

+7
source share
3 answers

The most comprehensive explanation is presented here .

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

The sample fragment uses this function to determine the current tangible area of ​​the child view (after the layout) in order to effectively expand it by creating TouchDelegate

+4
source

They should certainly be better documented. If you look at the source of View#getHitRect(Rect) , you will see that if the view has an “identification matrix” or is not attached to the window, it returns exactly what we think. An alternative branch means that the view has a transformation, so to get a “direct hit” for the parent, which is the smallest possible rectangle in its coordinate system that spans the view, you need to move the line to the beginning, start the transformation, and then return to its original position.

Therefore, you can use it as a shortcut for this purpose if there is no conversion. If there is a transformation, remember that you will receive values ​​in the rectangle, which can be outside or inside the view, as shown in the figure.

+1
source

The returned Rect contains 4 values:

  • lower
  • to the left
  • to the right
  • upper

bottom indicates the y coordinate of the bottom of the rectangle. left indicates the x coordinate of the left side of the rectangle. and etc.

Parent coordinate means that the values ​​of the rectangle fall into the parent coordinate system.

Imagine that you are standing in an open field at night, looking at the moon. Your position on earth can be expressed in many ways.

If we expressed your position in your local coordinate system, you would be located at (latitude / longitude) (0, 0). When you go around the neighborhood, your local coordinate system never changes; you are always at the center (0,0) in your local coordinate system.

However, if we expressed your location using the Earth's coordinate system, you may be in (latitude, longitude) (16, 135).

You are standing on the earth, therefore the earth is your parental coordinate system.

In the same way, a view can be contained in LinearLayout. LinearLayout will be the parent of the View, so the values ​​from getHitRect () will be expressed in the LinearLayout coordinate system.

EDIT

In general terms, a hit rectangle is a term used to define a rectangular area used to detect collisions. From an Android perspective, a hit rectangle is just an instance of the Rect type.

getLeft() methods, etc. are just data accessories in Rect, so Rect members define the same rectangle you would get by calling methods.

A typical usage scenario for Rect will handle tap events:

 //Imagine you have some Rect named myRect //And some tap event named event //Check if the tap event was inside the rectangle if(myRect.contains(event.getX(), event.getY()){ //it a hit! } 

You can also see if two rectangles intersect with each other.

 if(myRect.contains(myRect2)){ //collision! } 

To represent a rectangle with a delete is not used directly, you can see the source . The upper, lower, left and right values ​​use tons in the view, but the getHitRect () method is much more convenient to pass these parameters (top / bottom / left / right) to people who need them in a neat package.

0
source

All Articles