Detailed introduction to Android application coordinate system

Detailed explanation of Android application coordinate system:

As the recent Android project requires the knowledge of coordinate system, Du Niang took a look, sorted out the relevant materials and recorded them.

1 background

Last year, many people told me in private letters that they wanted to talk about custom controls. In fact, many blogs on the Internet talked about all kinds of custom controls, but most of them were given fish, but there were few systematic articles on fish. At the same time, because they didn't have time to plan this series of articles, they wanted to mention this series of articles again recently, So let's first summarize a core knowledge point of custom controls - coordinate system.

Many people may despise Android's coordinate system, but if you want to completely learn custom controls, I want to say that understanding the coordinate meaning of Android's various coordinate systems and some APIs is definitely a small skill that can not be ignored; In fact, most of the rewriting of the main onxxx () methods of the so-called Android custom view are dealing with coordinate logic operations, so let's talk about the Android coordinate system first.

2. Android coordinate system

When it comes to Android, the coordinate system is actually a three-dimensional coordinate, z-axis up, X-axis right and y-axis down. The point processing of these three-dimensional coordinates can form Android rich interface or animation effects, so the Android coordinate system is the size sketch of a building in the whole Android interface. Let's take a look at these related concepts.

2-1 Android screen area division

Let's take a look at a picture to understand the area division of the Android screen

As follows:

Through the above figure, we can intuitively see Android's definition of screen division. Here we give some coordinates or measurement methods of common areas in these areas. As follows:

Special note: these methods are best deployed in Activity's onWindowFocusChanged () method or later, because only this time is the real display of OK, and I can't read my previous blog about setContentView.

2-2 Android view absolute relative coordinate system

We have analyzed the division of Android screens above. We can find that the focus of our usual development is actually focusing on the view layout area. Let's talk about various coordinate systems related to the view area in detail. Look at the following picture first:

Through the above figure, we can intuitively explain some coordinate related methods of view, but it must be clear that the above methods must be effective after layout, as follows:

At the same time, it can also be seen that some methods provided by motionevent when a finger touches the screen are explained in the above figure, as follows:

The above explains the meaning of various getxxx methods for mathematical and logical operation judgment in many codes. However, the above just mentioned some relatively static Android coordinate point relationships. Let's take a look at several view methods closely related to the above methods. As follows:

The above explains the meanings of obtaining various widths and heights when customizing the view. Next, let's take a look at some methods for the view to obtain the position in the screen, but these methods can only be used after the onwindowfocuschanged() method of the activity. As shown below:

Next, we will give the results of some coordinate methods of view involved in the above figure (the results adopt the actual coordinates returned by the method, and do not rely on the above actual absolute coordinate conversion. The above absolute coordinates are only used to illustrate the position in the example), as follows:

The commonly used methods and meanings of static coordinate acquisition and processing of related views have been described. Let's take a look at some dynamic explanations (the so-called dynamic and static is just my personal name).

2-3 Android view animation related coordinate system

In fact, when we use animation, especially when making up room animation, you will find that many coordinate parameters are involved, one is relative, the other is absolute, and you may have various masks. Then you might as well take a look at the blog "detailed explanation of the use of all animation in Android application development", which introduces in detail the coordinate system related to Android animation, which is no longer cumbersome here.

2-4 Android view sliding related coordinate system

Another group of common and important methods closely related to coordinates provided by view are related to scrolling or sliding. We will give relevant explanations below (special note: the scrollto() and scrollby() of view are used to slide the content in the view rather than change the position of view; you can use offsetleftandright() and offsettopandbottom() to change the position of view in the screen) Method, which causes the getleft () equivalent to change.), As follows:

Many people may have doubts about the feature that the scrollby() and scrollto() parameters of Android view pass a positive number but move in the negative direction of the coordinate system, or even memorize the conclusion. Here we briefly give the real reason for this feature - source code analysis, as follows:

The method annotation of view clearly states that it will trigger onscrollchanged() and invalidated() methods. Then we will turn to the draw() process triggered by the invalidated() method. In the draw() process, the following invalidate() method will eventually be triggered, as follows:

The core is here. I believe you know what's going on without my explanation. Make up your own brain.

Special attention to the scrollto() and scrollby() methods: if you call the scrollto() method for a ViewGroup, you can scroll the contents of the ViewGroup. If you want to scroll a ViewGroup, you can nest an outer layer and scroll the outer layer.

3 Summary

It can be found that the above only explains some concepts related to coordinates commonly used in view. Understanding and learning these coordinate concepts about custom controls is only a foundation and a foreshadowing for subsequent content. Therefore, it is necessary to fully understand this part before continuing to expand and learn new things.

There are some other methods related to coordinate acquisition in view, but they are generally not commonly used, so you can check the API or debug to see the phenomenon for learning at that time. Due to the limited space and time here, they will not come together.

Thank you for reading, hope to help you, thank you for your support to this site!

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
分享
二维码
< <上一篇
下一篇>>