Recently, it was reported that six former middle and senior leaders of Huawei, who brought internal information to LeEco and Coolpad, have been detained and are awaiting the approval of the procuratorate. But Huawei said: at present, it does not involve LeEco and Coolpad, and does not involve huawei’s middle and senior management, only huawei engineers and designers. Coolpad and LeEco officials revealed that the arrested employees had left Huawei to start their own businesses, but after the failure of the business, they joined Coolpad and LeEco. They were also arrested because of some patent disputes when they started their own businesses.

This is the third article submitted by Xu Jun, mainly with you to talk about some methods related to View coordinates. Hopefully that will help you understand better.

Xu Jun’s blog address:

http://blog.csdn.net/gdutxiaoxu

View, for us in the familiar, since contact Android, we have been in contact with View, the interface is full of views, such as TextView, Button, LinearLayout and so on we often use, but do we really understand View? In particular, the View coordinates.

Do you really understand the concepts of mLeft, mRight, mY, mX, mTranslationY, mScoollY relative to the screen? If you are, then you don’t need to read this blog post. If you are still a bit vague, take a few minutes to read it.

Why write this blog post? Because it is important to master the coordinates of a View, especially for custom views, learning animation is of great significance. This blog post focuses on the question:

  • View getLeft() and getRight() and getTop() and getBottom()

  • The connection between View getY(), getTranslationY(), and getTop()

  • View 的 getScroollY 和 View 的 scrollTo() 和 scrollBy()

  • Event. GetY () and event. GetRawY ()

  • Extension, how to get the height of the StatusBar and titleBar

The largest grassy green area is the screen interface, the red sub-large area is called the “application interface area”, and the smallest purple area is called the “View Drawing area”. The top part of the screen, outside the application interface area, displays the phone’s battery network operator information as the “status bar”, and the top part of the application area, outside the View drawing area, displays the Activity name as the “title bar”.

From this picture, we can see that in Android, when ActionBar is present:

Screen height = Status bar + Application area height = Status bar height + (Title bar height + View drawing area height)

When ActionBar does not exist:

Screen height = Status bar + Application area height = Status bar height + (View drawing area height)

View getLeft() and getRight() and getTop() and getBottom()

Top is the upper-left ordinate, left is the upper-left ordinate, right is the lower-right ordinate, and bottom is the lower-right ordinate, all relative to its immediate parent View, not relative to the screen. Make a clear distinction. So which coordinates are relative to the screen, and how do we get coordinates relative to the screen?

None of the variables in the View are relative to the screen, but we can get coordinates relative to the screen. In general, we need to get the coordinates and height of the View until the View is drawn, which is not available in the Activity’s onCreate() method. You must wait until the View is drawn to get the coordinates of the response to the View. Generally speaking, there are two main methods.

The first method, onWindowFocusChanged(), is called:



The second method is to measure when the view tree is drawn:



The connection between View getY(), getTranslationY(), and getTop()

  • GetY ()

Added in API level 14

Pixels The visual y position of this view, in Pixels. The default value is The same as getTop()

  • getTranslationY()

Added in API level 14

The vertical position of this view is relative to its top position, in pixels.

What does getY() have to do with getTranslationY() and getTop()?



GetY () = getTranslationY()+ getTop(), the default value of getTranslationY() is 0, unless we change it by setTranlationY(). So that’s the default value of getY() that we went up to is the same as getTop()

So how do we change top and Y? It is obvious that the corresponding set methods, setY() and setTop(), can be called to change their values.

View 的 getScroollY() 和 View 的 scrollTo() 和 scrollBy()

GetScrollY () is a special function because it involves a value called mScrollY. In short, getScrollY() usually gets 0 unless you’ve called scrollTo or scrollBy to change it.

  • ScrollTo () and scrollBy ()

ScrollTo () means where to slide, and scrollBy() means how much to slide relative to the current position. Of course, this can also be reflected in the source code:



There are a few things to note:

1. Either scrollTo or scrollBy is actually scrolling the contents of the View, not the View itself. You can do a little experiment. Call scrollTo or scrollBy, and always move the blue subLinearLayout.

For example, scrollTo(-100,0), the View moves in the positive direction of the X axis. This is inverted because it is not the opposite. For the source analysis of these two functions, you can see:

View scrollBy() and scrollTo() arguments

http://blog.csdn.net/xplee0576/article/details/24242383

View 的 width 和 height



We can see that the Android height is derived from mBottom and mTop. How do we set the Android height?

Android :height= “android:height=” android:height=” You might think of the setWidth() method, right? The setWidth() method is not found. How do we dynamically set height? There are actually two ways:



The second method, which is not recommended, is to change the value of top or bottom individually.

As for width, it is basically the same as height, except that it is determined by mRight and mLeft.

LayoutParams is not recommended to change the state of the View during animation, because changing LayoutParams will call requestLayout(), which marks the current View and its parent container, and will be submitted layer by layer. Until ViewRootImpl processes the event, ViewRootImpl will call three processes, starting from measure, for each view containing marker bits and its sub-views will measure, layout and draw, the performance is poor, the source code is as follows: More analysis of the requestLayout() method can be found in this blog post:

Android View in-depth analysis of requestLayout, Invalidate and postInvalidate

http://blog.csdn.net/a553181867/article/details/51583060



Therefore, if you want to change the state of the View during animation execution after API 14, it is recommended to use setTranslationY() and setTranslationX() and avoid changing LayoutParams as much as possible. Because of poor performance.

Event. GetY () and event. GetRawY ()

Distinguish between motionEvent.getrawx () and motionEvent.getx ()

In public Boolean onTouch(View View, MotionEvent event), x,y are relative to the upper left point of the control (the control itself) when you touch it. Rawx, rawy is always relative to the screen. GetX () is the Widget’s X coordinate relative to its top-left corner, while getRawX() is the Widget’s X coordinate relative to the top-left corner of the screen (note: this screen is the top-left corner of the phone screen, regardless of whether the Activity has a TitleBar or is full-screen).

Extension, how to get the height of the StatusBar and titleBar



The important thing to note here is that in the presence of the ActionBar, this is how we get the height of the titleBar, otherwise we can’t get it because viewTop is 0.

Study tired every day, read some funny jokes to relax. Pay attention to the most entertaining public number, have a good mood every day.

If you have a good technical article to share with you, welcome to contribute to my official account, please click “contribution” menu on the official account home page for details.

Please long press the following picture -> the QR code in the identification picture or scan to follow my official account: