Coordinate system

The difference between screen coordinates and mathematical coordinates

As mobile devices generally define the upper left corner of the screen as the origin of coordinates, the right direction as the increase direction of x axis, and the downward direction as the increase direction of Y axis, the coordinate system on the mobile phone screen is slightly different from the coordinate system common in mathematics that the right direction is the increase direction of X axis and the upward direction is the increase direction of Y axis. Details are as follows:

(PS: where the Angle A is corresponding, note the y direction!)The one on the right is the actual coordinate system on the phone screen.

Coordinate system in Android

In Android, the screen, ViewGroup, and View all have their own reference coordinate system. As described above, their coordinate system is X-axis to the right, Y-axis down to guide the relevant drawing.

View position description

Now that we know about Android’s screen coordinate system, we can look at how controls are positioned in the coordinate system:

First of all, a control must be in the coordinates can be defined by the x, y, so for a control is bound to be in a reference frame, the reference frame is its parent control, determine the reference frame, you can define controls for position, but control is a rectangular area, how to define the area?

In Android, the upper left corner of the control is used as the reference point of the control to define the position of the control, which provides the following methods to describe it:

view.width              // View width
view.height             // View height

view.x                  // The X position of the upper left corner of View relative to the upper left corner of ViewGroup
view.y                  // The Y position of the upper left corner of View relative to the upper left corner of ViewGroup
view.z                  // Z position relative to the screen

view.left               // The left side of View relative to the left side of ViewGroup
view.top                // The distance from the View side to the VIewGroup side
view.right              // The distance between the right side of the View and the left side of the ViewGroup
view.bottom             // The distance between the bottom edge of the View and the top edge of the ViewGroup
view.elevation          // View height

view.translationX       // The distance between the leftmost position of the current View and the leftmost position of the original View during sliding
view.translationY       // The distance between the top of the View's current position and the top of the View's original position during sliding
view.translationZ       // During animation, the distance between the View's current z-axis height and the View's original z-axis height
Copy the code

It can be found that the position description of a View is relative to the ViewGroup it is in. Here is a picture to more intuitively feel the attributes of these position descriptions:

Since the related attributes of translation are not easy to express, here is the actual calculation formula of view animation:

x = left + translationX;
y = top + translationY;
z = elevation + translationZ;
Copy the code

The specific content will be introduced in detail in the chapters related to animation. Here is a brief introduction.

The position description of the touch point

Android programmers know that touches need to override the onTouch method, which contains a MotionEvent that provides a description of the position of the touch point:

event.x         // The touch point's X position relative to the View
event.y         // The Y position of the touch point relative to the View
event.rawX      // The distance of the touch point relative to the far left of the screen
event.rawY      // The distance of the touch point relative to the top of the screen
Copy the code

To get a more intuitive feel for it, use a legend:

View relative to the screen

Here are three methods to obtain the View distance from the screen, the specific implementation method is as follows:

getLocationInWindow

val position = IntArray(2)
viewGroup.getLocationInWindow(position)
Log.d("coordinate-screen", "x: " + position[0])
Log.d("coordinate-screen", "y: " + position[1])
Copy the code

getLocationOnScreen

val position = IntArray(2)
viewGroup.getLocationOnScreen(position)
Log.d("coordinate-screen", "x: " + position[0])
Log.d("coordinate-screen", "y: " + position[1])
Copy the code

getGlobalVisibleRect

val rect = Rect()
viewGroup.getGlobalVisibleRect(rect)
Log.d("coordinate-screen", "x: " + rect.left.toString())
Log.d("coordinate-screen", "y: " + rect.top.toString())
Copy the code

conclusion

The above is about the coordinate system and View in the coordinate system position description, if there is any explanation error or any questions, welcome to leave a comment.