Graphical analysis of Android coordinate system and view coordinate system

1. Android coordinate system

In Android, the top left corner of the screen is used as the origin of the Android coordinate system

From the origin to the right is the positive direction of the X axis, and from the origin down is the positive direction of the Y axis

View provides getlocationonscreen (int [] location) method to obtain the absolute coordinates in the whole screen, which is the coordinates of the upper left corner of view. Note that the coordinate value of the view is obtained from the upper left corner of the screen, so it also includes the height of the notification bar

The concrete implementation of this method

You can see that in the int [] array passed in, location [0] represents the x-axis coordinates and location [1] represents the y-axis coordinates

There is also a getlocationinwindow method to obtain the absolute coordinates of the view in the current window

The coordinates we obtained through the getrawx(), getrawy() method in the motionevent class also belong to the coordinates in this Android coordinate system

2. View coordinate system

The view coordinate system in Android describes the positional relationship between the child view and its parent view

Like the Android coordinate system, the view coordinate system takes the right of the origin as the positive direction of the X axis and the down of the origin as the positive direction of the Y axis. Different from the Android coordinate system, the origin of the view coordinate system takes the position of the upper left corner of the parent view as the origin

As shown in the figure above, for a button, the upper left corner of the parent view LinearLayout is the origin of the view coordinate system (0,0)

What we get through the geTx () and gety () methods in the motionevent class is the coordinates of the view coordinate system

In Android, the system provides many methods to obtain coordinate values and relative distances

API provided by view

• gettop(): get the distance from the top edge of the view to the top edge of its parent layout

• getleft(): get the distance from the left of the view to the left of its parent layout

• getright(): get the distance from the right side of the view to the left side of its parent layout

• getbottom(): get the distance from the bottom edge of the view to the top edge of its parent layout

API provided by motionevent

• geTx (): get the distance from the click position to the left of the view

• gety (): get the distance from the click position to the top edge of the view

• getrawx(): get the distance from the click position to the left of the screen

• getrawy(): get the distance from the click position to the top edge of the screen

The above graphic analysis of Android coordinate system and view coordinate system is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>