An in-depth analysis of Android coordinate system _android

Source: Internet
Author: User
Tags gety touch

1 background

Last year a lot of people DMs told me to let's talk about custom controls, in fact, many blogs on the network are talking about a variety of custom controls, but most of them are given to fish, but there are few more systematic articles on fishing, at the same time because they have no time to plan this series of articles, and recently want to put this series of articles, So let's start by summarizing one of the core knowledge points of a custom control--the coordinate system.

Many people may be dismissive of Android's coordinate system, but if you want to learn to customize the controls completely, I'd like to say that understanding the coordinates of Android's various coordinate systems and some APIs is definitely a small, but not negligible, skill; The main onxxx of the so-called Android Custom view () In fact, most of the rewriting of the method is in the process of coordinate logic operations, so let's talk about the Android coordinate system first.

2 Android coordinate system

Speaking of the Android coordinate system is actually a three-dimensional coordinate, z-axis up, x-axis to the right, y-axis downward. This three-dimensional coordinate point processing can make Android rich interface or animation effects, so the Android coordinate system in the entire Android interface is the building of the size of the sketch, let's look at these related concepts.

2-1 Android Screen Area division

Let's take a look at the map to find out about the area of the Android screen (for an in-depth discussion of this thing you can look at the "Android application Setcontentview and Layoutinflater loading parsing mechanism source analysis" article, which gives some explanation of the principles), As follows:

We can see the Android definition of the screen visually from the above figure. Here we give some coordinates or metrics for the areas that are commonly used in these areas. As follows:

Gets the width and high dimensions of the screen area to obtain
displaymetrics metrics = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (metrics);
int widthpixels = metrics.widthpixels;
int heightpixels = metrics.heightpixels;
Application app area wide and high size get
Rect Rect = new Rect ();
GetWindow (). Getdecorview (). Getwindowvisibledisplayframe (rect);
Gets the status bar height
Rect rect= new Rect ();
GetWindow (). Getdecorview (). Getwindowvisibledisplayframe (rect);
int statusbarheight = rectangle.top;
View layout area wide and high size get
Rect Rect = new Rect (); 

Special Note: These methods are best to be onwindowfocuschanged () or later in the activity, because only then is the real display OK, Do not understand can see my previous about Setcontentview related blog.

2-2 Android View Absolute relative coordinate system

Above we analyze the Android screen division, we can find that the focus of our development is actually focused on the view layout area, then we will go into the view area related to the various coordinate systems. Let's look at the following picture:

From the above we can intuitively give a view of some coordinate-related methods to explain, but it must be clear that these methods must be layout after the effective, as follows:

static coordinate method of view explain
GetLeft () Returns the distance from the left side of the view itself to the left of the parent layout
GetTop () Returns the distance from the top edge of the view itself to the top of the parent layout
GetRight () Returns the distance to the right of the view itself to the left of the parent layout
Getbottom () Returns the distance from the bottom edge of the view to the top of the parent layout
GetX () The return value is GetLeft () +gettranslationx (), when Settranslationx () is GetLeft () invariant, GetX () is changed.
GetY () The return value is GetTop () +gettranslationy (), when Settranslationy () is gettop () invariant, GetY () is changed.

It is also possible to see some of the method explanations provided by the motionevent when the finger touches the screen in the image above, as follows:

Motionevent Coordinate method explain
GetX () Current touch event distance to the left of the current view
GetY () Current touch event distance from top edge of current view
GETRAWX () The distance from the current touch event to the left of the entire screen
Getrawy () The distance between the current touch event and the top edge of the screen

It explains what you see in a lot of code, the meaning of a variety of getxxx methods for mathematical logic operations judgments. But it's just some relatively static android coordinates, so let's look at a couple of view methods that are closely related to the above method. As follows:

View Width High method explain
GetWidth () After layout, the return value is Mright-mleft, which is generally referred to as measure width (measure may not be useful), but is not required.
GetHeight () After layout, the return value is Mbottom-mtop, which will generally refer to the measure height (measure may not be useful), but not necessarily.
Getmeasuredwidth () It may be useless to return the mmeasuredwidth value of the measure process for layout reference.
Getmeasuredheight () It may be useless to return the mmeasuredheight value of the measure process for layout reference.

This explains some of the various ways to get the width and height of a custom view, but let's take a look at some of the methods for the view to get the position on the screen, but these methods need to be used after the onwindowfocuschanged () method of the activity. The following figure:

We will give the results of some of the coordinate methods of the view mentioned above (the result is to use the actual coordinates returned by using the method, not relying on the actual absolute coordinate conversion above, the above absolute coordinates are just to illustrate the position in the example), as follows:

the method of view above View1 results above VIEW2 results Conclusion Description
Getlocalvisiblerect () (0, 0-410, 100) (0, 0-410, 470) Gets the coordinate area visible to the view itself, coordinates with its own upper-left corner as the origin (0,0), and the other is the coordinates of the visible region's lower-right corner relative to its own (0,0) point, in fact View2 's current height is 550, and the height is 470.
Getglobalvisiblerect () (30, 100-440, 200) (30, 250-440, 720) Gets the viewable area of the view in the absolute coordinate system of the screen, the coordinates in the upper-left corner of the screen as the origin (0,0), and the other as the coordinates of the visible area's lower-right corner relative to the screen origin point (0,0).
Getlocationonscreen () (30, 100) (30, 250) The coordinates are relative to the entire screen, and the Y coordinate is the distance from the top left corner of the view to the bottom of the screen.
Getlocationinwindow () (30, 100) (30, 250) If you are a normal activity, the Y coordinate is the view upper-left corner to the top of the screen (when window is as big as the screen), or the y-coordinate is the distance from the top-left corner of the title bar of the current dialog mode activity for the dialog-type activity.

To this commonly used related view static coordinates to obtain the processing method and the meaning all has already narrated, below we look at some dynamic explanation (the so-called movement is only my personal name).

2-3 Android View animation related coordinate system

In fact, when we use animation, especially motion tween, you will find that it involves a lot of coordinate parameters, for a while for the relative, a moment for absolute, you may be a variety of masks. Then look at the Android application development of all animation use of this blog, which details on the Android animation related to the coordinate system, there is no longer cumbersome narrative.

2-4 Android View sliding correlation coordinate system

Another common set of important methods that view provides about coordinates is scrolling or sliding related, and here's how to explain it (note: the Scrollto () and Scrollby () of view are used to slide the contents of the view, not to change the view's position. Changing the position of the view in the screen can use the Offsetleftandright () and the Offsettopandbottom () method, which will cause the getleft () equivalent to change. ), as follows:

sliding method of view Effect and Description
Offsetleftandright (int offset) The horizontal direction moves view,offset is the regular X axis forward movement, moves is the entire view,getleft () to change, the custom view is very useful .
Offsettopandbottom (int offset) The vertical shift View,offset is a regular y-axis forward move, and the entire view,gettop () is changed, and custom view is useful .
Scrollto (int x, int y) Slide the contents of the view (not the entire view) to the appropriate position, the reference coordinate origin is the upper-left corner of the Parentview, and the x,y is moving in the opposite direction to the XY axis, and vice versa.
Scrollby (int x, int y) Continue to slide XY on the basis of Scrollto ().
SETSCROLLX (int value) The substance is scrollto (), just changing the y-axis slide.
setscrolly (int value) The essence is Scrollto (), just change the x axis slide.
GETSCROLLX ()/getscrolly () Gets the current slide position offset.

About Android View the Scrollby () and Scrollto () parameters pass a positive number and move in the negative direction of the coordinate system. Many people may have doubts, even rote conclusions, here we simply give the real reason for this feature-source analysis, as follows:

public void Scrollto (int x, int y) {
if (mscrollx!= x | | mscrolly!= y) {
int oldx = MSCROLLX;
int oldy = mscrolly;
MSCROLLX = x;
mscrolly = y;
Invalidateparentcaches ();
Onscrollchanged (MSCROLLX, mscrolly, OLDX, oldy);
if (!awakenscrollbars ()) {
postinvalidateonanimation ();}}}

The view's comment on the method clearly shows that the allocation he triggers the onscrollchanged () and the invalidated () method, then we turn the target to the draw () process triggered by the invalidated () method, Draw () The following invalidate () method is eventually triggered in the process, as follows:

public void invalidate (int l, int t, int r, int b) {
final int scrollx = MSCROLLX;
Final int scrolly = mscrolly;
The true reason why the parameters and coordinates are reversed when scroller
invalidateinternal (L-SCROLLX, t-scrolly, R-scrollx, b-scrolly, True, false);

The core is here, I believe you do not need me to explain that we also know what happened, the brain repair.

The Scrollto () and Scrollby () methods pay special attention to: if you give a viewgroup call to the Scrollto () method to scroll the contents of the ViewGroup, if you want to scroll a viewgroup then give him an outer layer, rolling the outer layer.

3 Summary

It can be found that the above is only a description of some of the commonly used in the view of the coordinate-related concepts, about custom controls to learn these coordinate concepts is only a foundation, but also a follow-up content of the bedding, so it is necessary to thoroughly understand this part of the content to continue to expand learning new Dongdong.

View there are some other methods related to coordinate acquisition, but generally less commonly used, so you can check the API or debug to learn the phenomenon, where space and time is limited to one by one.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.