Android application coordinate system fully specific explanation

Source: Internet
Author: User
Tags gety

1 Background

Last year, there were a lot of private messages telling me to let me talk about my own definition of the control, in fact, many of the blogs on the concept viewing Mencius network are talking about their own definition controls, but most of them are awarded with fish. But very few more systematic to give the article in the fishing, at the same time because they have not time to plan this series of articles, the recent want to this series of articles again, so come to the first summary of their own definition of the control of a core knowledge point-coordinate system.

A lot of people may be dismissive of Android's coordinate system. But assuming you want to learn to define your own control thoroughly, I would say that understanding the coordinate meanings of Android's various coordinate systems and some APIs is definitely a small but not negligible skill. The so-called Android itself defines the overriding nature of the view's major onxxx () methods in fact, most of them are dealing with coordinate logic operations, so let's start with the Android coordinate system.

"Craftsman Joshui http://blog.csdn.net/yanbober without consent is prohibited reprint." Please respect the results of the work of the author. Private Messages Contact Me "

2 Android coordinate system

Speaking of the Android coordinate system is actually a three-dimensional coordinate, Z axis upward, x axis right, Y axis downward.

This three-dimensional coordinate point processing can make up the Android rich interface or animation and other effects. So the Android coordinate system is a sketch of the size of the building in the entire Android interface. Let's take a look at these related concepts below.

2-1 Android Screen area Division

Let's take a look at the map to see the area of the Android screen (for an in-depth discussion of this thing you can look at the article "Android application Setcontentview and Layoutinflater load parsing mechanism source code Analysis", which gives some explanation of the principle). For example, the following:

We can visually see the definition of Android for screen partitioning. Here we give some of the coordinates or measurements of the areas that are used frequently in these areas.

For example, the following:

//获取屏幕区域的宽高等尺寸获取new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(metrics);int widthPixels = metrics.widthPixels;int heightPixels = metrics.heightPixels;
//应用程序App区域宽高等尺寸获取new Rect();getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
//获取状态栏高度new Rect();getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);int statusBarHeight = rectangle.top;
//View布局区域宽高等尺寸获取new Rect();  getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(rect);  

Special Note: the above methods are best used in the onwindowfocuschanged () method of the activity or after the allocation. Because only then is the real display OK, does not understand can see my previous about Setcontentview related blog.

2-2 Android View Absolute relative coordinate system

Above we analyzed the Android screen division, we can find that our usual development focus is actually focused on the view layout area, then we will describe the view area related to the various coordinate systems.

Let's look at the following picture:

We can give a very intuitive view of some of the coordinates related to the interpretation of the method, but it is necessary to understand that the above methods must be valid after layout. For example, the following:

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


At the same time can also be seen in giving a finger touch the screen when Motionevent provides some methods to explain. For example, the following:

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


The above explains what you see in a lot of code the various getxxx methods for mathematical logic operations inference. Just above is just a few relatively ingenious Android coordinate point relations, let's look at a few of the above methods closely related to the view method. For example, the following:

View Width Height method explain
GetWidth () After layout is valid, the return value is Mright-mleft. It is usually the width of the measure (measure may not be useful), but it is not required.
GetHeight () After layout is valid, the return value is mbottom-mtop. It is usually the height of the measure (measure may not be useful), but it is not required.

Getmeasuredwidth () Returns the Mmeasuredwidth value from the measure process for layout reference. Maybe it's useless.

Getmeasuredheight () Returns the Mmeasuredheight value obtained by the measure procedure. For layout references. Maybe it's useless.


The above explains some of the meanings of the various gain widths when you define a view, and here are some ways to see where the view gets on the screen, except that these methods need to be used after the onwindowfocuschanged () method of the activity.

For example, with:

Here we give the results of some of the coordinate methods of the view shown above (the actual coordinates returned by the usage of the result. Does not rely on the actual absolute coordinate conversion above, the absolute coordinates above are simply to illustrate the position in the sample. For example, the following:

gets the coordinate area that the view itself is visible, coordinates with its own upper-left corner as the origin (0,0), and one point is the coordinates of the lower-right corner of the visible area relative to its own (0,0) point, in fact View2 the current height is 550 and the visible height is 470.

Methods for
view View1 results View2 Results Conclusion Descriptive description
getlocalvisiblerect () (0, 0-410, up) (0, 0-410, 470)
getglobalvisiblerect () (100-440, $) (30, 250 -720) gets the viewable area of the view in the absolute coordinate system of the screen. The coordinates are the origin (0,0) in the upper-left corner of the screen, and a point is the coordinates of the lower-right corner of the visible area relative to the screen origin (0,0) point.
getlocationonscreen () (+) (+) coordinates are relative to the entire screen, The y-coordinate is the distance from the upper-left corner of the view to the top of the screen.
getlocationinwindow () (+, +) (+) assumed to be normal activit Y coordinates are the upper left corner of the view to the top of the screen (the window is as large as the screen), and if the activity of the dialog box is the Y coordinate is the distance from the top of the title bar of the current dialog mode activity to the upper left corner of the view.


The method and meaning of obtaining the processing of the static coordinates of the relevant view which is often used are already described, and we will look at some explanations of the dynamic (the so-called movement is only my personal address).

2-3 Android View animation related coordinate system

In fact, when we use animations, especially when motion tweens. You will find that it involves very many coordinate references, a moment for the opposite, and a moment for absolute. You may have a variety of hoops. Then it is best to look at the "Android application development of all animation use specific explanation" this blog. This is a detailed description of the Android animation related coordinate system, which is no longer cumbersome to describe.

2-4 Android View sliding correlation coordinate system

There is also a set of frequently used important methods that are related to the coordinates of the view provided by scrolling or sliding, and the following explanations are given ( Special note: View's Scrollto () and Scrollby () are used to slide the contents of the view, Instead of changing the position of the view, changing the position of the view on the screen can use the Offsetleftandright () and Offsettopandbottom () methods, which will cause the getleft () equivalent to change. ). For example, the following:

View sliding methods effects and descriptive narratives
Offsetleftan Dright (int offset) to nudge the view horizontally. Offset is the positive x-axis forward movement, moving the entire view,getleft () will change, own definition view is very useful .
offsettopandbottom (int offset) vertical Nudge View,offset is a regular y-axis forward movement. The whole view is moving. The GetTop () will change. defining view yourself is useful .
scrollTo (int x, int y) Slides The contents of the view (not the entire view) to the corresponding location, and the coordinates origin is parent In the upper-left corner of the view, X, Y moves in the opposite direction to the XY axis, and vice versa.
scrollby (int x, int y) continues to slide XY on the basis of Scrollto ().

setscrollx (int value) is essentially scrollto (), simply changing the y-axis slide.

setscrolly (int value) is Scrollto () in essence. Just change the x-axis slide.
getscrollx ()/getscrolly () gets the current sliding position offset.


About the Scrollby () and Scrollto () parameters of Android view passing positive numbers but moving in the negative direction of the coordinate system may be a lot of people have doubts, or even rote conclusions, here we simply give the real reason to produce such characteristics-source code analysis, such as the following:

publicvoidscrollTo(intint 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 approach to the gaze shows that the dispatching he will trigger the onscrollchanged () and the invalidated () method. Then we will turn to the invalidated () method triggered by the draw () process, the draw () process will eventually trigger the following invalidate () method, such as the following:

publicvoidinvalidate(intintintint b) {    finalint scrollX = mScrollX;    finalint scrollY = mScrollY;    //scroller时为何參数和坐标反向的真实原因    truefalse);}

The core is right here. I'm sure you don't have to explain it to me. The brain complements itself.

The scrollTo () and Scrollby () methods are particularly important: Suppose you give a viewgroup call to the ScrollTo () method to scroll through the contents of the ViewGroup, If you want to scroll a viewgroup, then give him a nested outer layer and roll the outer layer.

"Craftsman Joshui http://blog.csdn.net/yanbober without consent is prohibited reprint." Please respect the results of the work of the author. Private Messages Contact Me "

3 Summary

can find. The above is just a description of some of the concepts that are often used in view in terms of coordinates. About yourself defining controls learning these coordinate concepts is just a foundation. is also a perhaps content of the foreshadowing. Therefore, it is necessary to fully thoroughly understand this part of the content of talent to continue to expand learning new things.

There are other methods associated with coordinate acquisition in view, but they are generally less frequently used. So when you can check the API or debug to see the phenomenon of learning can be. There are no more than one by one lanes of space and time.

"Craftsman Joshui Http://blog.csdn.net/yanbober without permission to reprint, please respect the author's labor results."

Private Messages Contact Me "

Android application coordinate system fully specific explanation

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.