Android coordinate system understanding directly related to the back of your Android advanced part of the study, if there is no solid foundation, you customize the view will have a desire dissatisfaction, can't stop-want to understand but feel difficult, want to give up and feel reluctant.
The coordinate system in Android and how to get coordinates
There are two coordinate systems in Android, called the Android coordinate system and the view coordinate system, respectively. And there are some related methods that can get the coordinate values in the coordinate system. Only by figuring out the differences can you achieve them without making mistakes or getting the results you want.
One, Android coordinate system and view coordinate system
(1) Android coordinate system
Let's take a look at what the Android coordinate system looks like. Paste a picture to illustrate, as follows:
As shown, the Android coordinate system takes the top-left corner of the phone screen as the coordinate origin, from that point to the right x-axis positive direction, and from that point down to the y-axis positive direction. In touch events, use the GETRAWX () and Getrawy () methods
The coordinates obtained are the coordinate values under the standard of this coordinate system.
(2) View coordinate system
Another coordinate system in Android is called the View coordinate system, which describes the location of the child view in the parent view. One more picture to illustrate, as follows:
As shown, the view coordinate system is in the upper-left corner of the parent view as the origin of the coordinates. The corresponding Origin points to the right of the x-axis positive direction, and the origin down to the y-axis positive direction. In touch, the
The coordinate values obtained by GetX () and gety () are the coordinate values in the view coordinate system.
The methods of obtaining coordinate values and relative distances
In Android, there is a very rich way to get coordinate values and relative distances. However, when using these methods, it is important to understand which coordinate system is used to obtain the standard.
To illustrate these methods one by one, I have also prepared a picture, as follows:
The above diagram is a good illustration of where the coordinate values or distances from each method go. Note that there are three black boxes in the picture, the outermost one is the phone screen,
The middle tier is ViewGroup, and the inner layer is the view placed in the ViewGroup.
In fact, the method can be divided into two categories, one is the view provided by the method, a class is the method provided by Motionevent. The following are explained separately:
View provides methods for obtaining coordinates and distances:
GetTop () Gets the distance from the top edge of the view itself to the top edge of its parent layout
GetLeft () Gets the distance from the left side of the view itself to the left side of its parent layout
GetRight () Gets the distance from the right side of the view itself to the left side of its parent layout
Getbottom () Gets the distance from the bottom of the view itself to the top edge of its parent layout
Motionevent provides the method:
GetX () Gets the distance to the left of the Click event Distance control, which is the view coordinate
GetY () Gets the distance from the top edge of the Click event Distance control, which is the view coordinate
GETRAWX () Gets the distance from the left side of the entire screen of the Click event, which is the absolute coordinate
Getrawy () Gets the distance of the click event from the top edge of the entire screen, which is the absolute coordinate
Getscrolly () Gets the distance from the view coordinate origin to the view roll out screen
GETSCROLLX () similar to ditto
Android View Getx,gety ...