View is the base class for all controls in Android, whether it's a simple button and textview, or a complex relativelayout and ListView, whose base class is the view class, and ViewGroup inherits the view class. This means that the view itself can represent simple and complex controls and layouts that, through this relationship, form the structure of the view tree.
When working with view, especially when dealing with some of the events of a custom view, we often need to obtain such or such parameters as the target view. Here is a brief introduction to the various parameters of view and their usage.
The view has properties such as X, Y, Z, left, right, top, bottom, elevation, Translationx, Translationy, Translationz, RAWX, Rawy, etc. The meanings of these parameters and how to get them are described below.
It is important to note that the following method, if called directly in the activity's OnCreate () method, may return 0, because when the program is just started, when the view is just started, we are going to get the view's various positional parameters, of course it will return 0. The workaround is to obtain it in the onwindowfocuschanged () method of the activity, or you can create a new thread that thread.sleep () for a period of time before acquiring the parameters.
Left, right, top, bottom, elevation
These five parameters represent the distance from the original position of the view relative to the edge of its parent control:
- Left: The leftmost distance of the target view and the left side of the view's parent control, obtained through the View.getleft () method;
- Right: The leftmost distance from the rightmost side of the target view and the view's parent control, obtained through the View.getright () method;
- Top: The topmost distance from the top of the target view and the view's parent control, obtained through the View.gettop () method;
- Bottom: The bottom of the target view and the top-most distance of the view's parent control, obtained by means of the View.getbottom () method;
- Elevation: the z-axis height of the target view and the height of the z-axis of the view's parent control are obtained through the View.getelevation () method (this property is a new property added after Android 5.0).
Note : The five attribute values for view represent the original position coordinate values of the view, no matter where the view is moved or how much is scaled or rotated, these five values are permanent.
Translationx, Translationy, Translationz
These three parameters represent the distance at which the current position of the view is shifted relative to its original position when animating or sliding the view:
- Translationx: In the sliding process, the leftmost distance of the view's current position and the left side of the view's original position is obtained by means of the View.gettranslationx () method;
- Translationy: During the sliding process, the top edge of the view's current position and the topmost distance of the view's original position are obtained by means of the view.gettranslationy () method;
- Translationz: In the animation process, the z-axis height of the view's current position and the distance from the view's original z-axis height are obtained through the View.gettranslationz () method (this method is a new method added after Android 5.0).
X, Y, Z
These three parameters represent the distance of the current position of the view relative to its parent control:
- X: The leftmost distance of the current position of the target view and the left side of the view's parent layout, obtained through the View.getx () method;
- Y: The top edge of the current position of the target view and the topmost distance of the view's parent layout, obtained through the View.gety () method;
- Z: the z-axis position of the current position of the target view and the distance from the z-axis position of the view's parent layout are obtained through the View.getz () method (this method is a new method added after Android 5.0).
The formula for the relationship between the three parameters and the previous parameters is as follows:
- x = left + Translationx;
- y = top + translationy;
- z = elevation + Translationz;
Location parameters for "Android-view" View