View system (1): View position parameters

In the entire system of Android, View plays an indispensable role in it.

foreword

My understanding of View in Android is: View literally refers to the view, or more precisely, it refers to the control, but the function of this control is more extensive, not as refined as the control Button. Why do I understand it this way? Because in the View system, for example Button, TextView... they are all inherited from View, such as in the Android 5.0 source code:

public class Button extends TextView
public class TextView extends View implements ViewTreeObserver.OnPreDrawListener

In Android, in addition to View, there is also ViewGroup. The literal understanding of ViewGroup is: it refers to the control group. Based on the analysis of View above, ViewGroup is well understood: ViewGroup is a collection of multiple controls. That is to say, ViewGroup can contain multiple View controls, so you can analyze why the following controls can contain multiple sub-Views:

public class LinearLayout extends ViewGroup
public class RelativeLayout extends ViewGroup
public class FrameLayout extends ViewGroup

Of course, ViewGroup is also inherited from View:

public abstract class ViewGroup extends View implements ViewParent, ViewManager

1.0 View's Positional Parameters

When preparing to understand the View system in depth, you need to understand some details of View, such as x, y, translationX, translationYand so on. When understanding the positional parameters of View, we need to figure out what the left, right, top, in View bottomrepresent:

/**
    * The distance in pixels from the left edge of this view's parent
    * to the left edge of this view.
    * 翻译:mLeft 值的大小是指从 View 的父容器的左边缘到该 View 的左边缘的距离,该值的单位为像素。
    */
protected int mLeft;
/**
    * The distance in pixels from the left edge of this view's parent
    * to the right edge of this view.
    * 翻译:mRight 值的大小是指从 View 的父容器的左边缘到该 View 的右边缘的距离,该值的单位为像素。
    */
protected int mRight;
/**
    * The distance in pixels from the top edge of this view's parent
    * to the top edge of this view.
    * 翻译:mTop 值的大小是指从 View 的父容器的上边缘到该 View 的上边缘的距离,该值的单位为像素。
    */
protected int mTop;
/**
    * The distance in pixels from the top edge of this view's parent
    * to the bottom edge of this view.
    * 翻译:mTop 值的大小是指从 View 的父容器的上边缘到该 View 的下边缘的距离,该值的单位为像素。
    */
protected int mBottom;

Then the above four values ​​of left, right, top, can be expressed as:bottom

Android coordinate system

In the source code of View, there are acquisition interfaces of left, right, top, as follows in the source code of Android 5.0:bottom

...
/**
    * Top position of this view relative to its parent.
    *
    * @return The top of this view, in pixels.
    */
public final int getTop() {
    
    
    return mTop;
}

...

/**
    * Bottom position of this view relative to its parent.
    *
    * @return The bottom of this view, in pixels.
    */
public final int getBottom() {
    
    
    return mBottom;
}

...

 /**
    * Left position of this view relative to its parent.
    *
    * @return The left edge of this view, in pixels.
    */
public final int getLeft() {
    
    
    return mLeft;
}

...

/**
    * Right position of this view relative to its parent.
    *
    * @return The right edge of this view, in pixels.
    */
public final int getRight() {
    
    
    return mRight;
}

...

In the Android window, there is a Cartesian coordinate system. This Cartesian coordinate system can be understood in this way, with the upper left corner of the mobile phone as the origin, the upper left corner is the X axis horizontally to the right, and the positive direction is from left to right; the upper left corner is vertically downward is the Y axis, and the positive direction is from top to bottom , as shown in the coordinate system above. Generally, this coordinate system runs through the View system, which we Android developers should be familiar with.

Find out the meaning of the four values ​​of left, right, top, bottomand then the meaning of the value is relatively simple, as shown in the following source code:XY

/**
* The visual x position of this view, in pixels. This is equivalent to the
* {@link #setTranslationX(float) translationX} property plus the current
* {@link #getLeft() left} property.
*
* @return The visual x position of this view, in pixels.
*/
public float getX() {
    
    
    return mLeft + getTranslationX();
}

The method getX()returns a single-precision floating-point value. From the source code above, the Xvalue of is mLeft + getTranslationX()the sum of and mLeftthe value of is the distance from the left edge of the View to the left edge of the parent View. From getTranslationX()the source code,

/**
    * The horizontal location of this view relative to its {@link #getLeft() left} position.
    * This position is post-layout, in addition to wherever the object's
    * layout placed it.
    *
    * @return The horizontal position of this view relative to its left position, in pixels.
    */
public float getTranslationX() {
    
    
    return mRenderNode.getTranslationX();
}

getTranslationX()In the method, it mRenderNode.getTranslationX()returns the value of the corresponding View through further calls translationX. In View, translationXthe value refers to the offset of the View on the X axis according to itself, and the default value is 0px; similarly, translationYthe value refers to the offset of the View on the Y axis according to itself , the default value is 0px. It is more clearly expressed by drawing:

Now I understand translationX, and translationYthe same principle is available, so we can see from the source code that mLeft + getTranslationX()the added value is X, and Yis mTop + getTranslationY(), so (X,Y)it can be regarded as the origin of the upper left corner of the VIew, and this (X,Y)is relative to the movement of the View change.

After understanding the origin of the four parameters, left, , , it is easier to understand rightView 's and :topbottomWidthHeight

width = mRight - mLeft;
height = mBottom - mTop;

In essence, the same is true for the analysis of View's Widthand Heightabove in the source code of Android 5.0. And View also provides interfaces to get Widthand : andHeightgetWidth()getHeight()

Sometimes, in the process of customizing View, you often encounter rawXthese rawYtwo parameters, such as the following kotlin source code:

class CustomSlidView: View {
    
    

    private var mLastX = 0f
    private var mLastY = 0f

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)


    override fun onTouchEvent(event: MotionEvent?): Boolean {
    
    

        val rawX = event!!.rawX
        val rawY = event!!.rawY

        when(event!!.action){
    
    
            MotionEvent.ACTION_MOVE -> {
    
    

                val deltaX = rawX - mLastX
                val deltaY = rawY - mLastY

                translationX += deltaX
                translationY += deltaY
            }
        }

        mLastX = rawX
        mLastY = rawY

        return true
    }
}

This CustomSlidViewimplements a View that can be dragged freely, and the inside (rawX,rawY)refers to the coordinate point when the finger presses the screen, and this coordinate point is relative to ``DecorView 而言的。(值得注意的是,left right top bottom 这四个值是相对于该 View 的父 View 的,而(X,Y)` is relative to itself)

2.0 Summary

This article mainly introduces several important positional parameters of View.

Guess you like

Origin blog.csdn.net/HongHua_bai/article/details/86727179