View中有关位置的属性--getX、getScrollX、getLeft、getWidth等,基于sdk20

论坛 期权论坛 脚本     
匿名网站用户   2020-12-20 03:21   11   0

View中的属性也是挺多的,搞得初学者都有点懵逼,今天就来捋一捋。

1、mLeft、mTop、mRight、mBottom定义了控件的区域(左上角和右下角的坐标),都有对应的get、set方法,其中mLeft表示控件左边距离父控件左边的距离,mRight表示控件右边距离父控件左边的距离,mTop和mBottom类似;

2、getWidth和getHeight,源码如下:

public final int getWidth() {
    return mRight - mLeft;
}
public final int getHeight() {
    return mBottom - mTop;
}
由上面的源代码可以发现宽高其实是由mRight-mLeft以及mBottom-mTop计算出来的,看到这里估计大家就知道为什么这两个方法在layout方法之后就可以正常获取到控件的宽高了吧,原因很简单:因为layout之后mRight,mLeft,mTop,mBottom就确定了。

3、getMeasureWidth和getmeasureHeight,获取控件的测量值,在onMeasure方法之后就可以获取正常值,其中onMeasure方法中的核心方法是setOnMeasureDimension。

4、getX、setX、getY和setY方法,源码如下:

/**
 * 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 + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
}
/**
 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
 * {@link #setTranslationX(float) translationX} property to be the difference between
 * the x value passed in and the current {@link #getLeft() left} property.
 *
 * @param x The visual x position of this view, in pixels.
 */

public void setX(float x) {
    setTranslationX(x - mLeft);
}
从上面的注释可以发现关键字visual,getx和setx方法其实是获取控件可见部分在以父控件左上角为原点、以向下为y正方向、以向右为x正方向的坐标系的坐标值,同理getY和setY一样。

5、getScrollX和getScrollY、setScrollX和setScrollY。

/**
 * Return the scrolled left position of this view. This is the left edge of
 * the displayed part of your view. You do not need to draw any pixels
 * farther left, since those are outside of the frame of your view on
 * screen.
 *
 * @return The left edge of the displayed part of your view, in pixels.
 */
public final int getScrollX() {
    return mScrollX;
}
从上面可以看到,这个其实返回的是mScrollX属性,而这个属性表示控件内容的距离左边的滚动距离,特别说明一下,这个属性值的坐标系是以控件左上角为原点、向上为y正轴、向左为x正轴。scrollTo和scrollBy两个方法与之密切相关,并且这两个方法的执行会触发onScrollChanged回调。

6、TransformationInfo静态内部类,封装了与View变化相关的属性。

static class TransformationInfo {
    /**
     * The transform matrix for the View. This transform is calculated internally
     * based on the rotation, scaleX, and scaleY properties. The identity matrix
     * is used by default. Do *not* use this variable directly; instead call
     * getMatrix(), which will automatically recalculate the matrix if necessary
     * to get the correct matrix based on the latest rotation and scale properties.
     */
    private final Matrix mMatrix = new Matrix();

    /**
     * The transform matrix for the View. This transform is calculated internally
     * based on the rotation, scaleX, and scaleY properties. The identity matrix
     * is used by default. Do *not* use this variable directly; instead call
     * getInverseMatrix(), which will automatically recalculate the matrix if necessary
     * to get the correct matrix based on the latest rotation and scale properties.
     */
    private Matrix mInverseMatrix;

    /**
     * An internal variable that tracks whether we need to recalculate the
     * transform matrix, based on whether the rotation or scaleX/Y properties
     * have changed since the matrix was last calculated.
     */
    boolean mMatrixDirty = false;

    /**
     * An internal variable that tracks whether we need to recalculate the
     * transform matrix, based on whether the rotation or scaleX/Y properties
     * have changed since the matrix was last calculated.
     */
    private boolean mInverseMatrixDirty = true;

    /**
     * A variable that tracks whether we need to recalculate the
     * transform matrix, based on whether the rotation or scaleX/Y properties
     * have changed since the matrix was last calculated. This variable
     * is only valid after a call to updateMatrix() or to a function that
     * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
     */
    private boolean mMatrixIsIdentity = true;

    /**
     * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
     */
    private Camera mCamera = null;

    /**
     * This matrix is used when computing the matrix for 3D rotations.
     */
    private Matrix matrix3D = null;

    /**
     * These prev values are used to recalculate a centered pivot point when necessary. The
     * pivot point is only used in matrix operations (when rotation, scale, or translation are
     * set), so thes values are only used then as well.
     */
    private int mPrevWidth = -1;
    private int mPrevHeight = -1;

    /**
     * The degrees rotation around the vertical axis through the pivot point.
     */
    @ViewDebug.ExportedProperty
    float mRotationY = 0f;

    /**
     * The degrees rotation around the horizontal axis through the pivot point.
     */
    @ViewDebug.ExportedProperty
    float mRotationX = 0f;

    /**
     * The degrees rotation around the pivot point.
     */
    @ViewDebug.ExportedProperty
    float mRotation = 0f;

    /**
     * The amount of translation of the object away from its left property (post-layout).
     */
    @ViewDebug.ExportedProperty
    float mTranslationX = 0f;

    /**
     * The amount of translation of the object away from its top property (post-layout).
     */
    @ViewDebug.ExportedProperty
    float mTranslationY = 0f;

    /**
     * The amount of scale in the x direction around the pivot point. A
     * value of 1 means no scaling is applied.
     */
    @ViewDebug.ExportedProperty
    float mScaleX = 1f;

    /**
     * The amount of scale in the y direction around the pivot point. A
     * value of 1 means no scaling is applied.
     */
    @ViewDebug.ExportedProperty
    float mScaleY = 1f;

    /**
     * The x location of the point around which the view is rotated and scaled.
     */
    @ViewDebug.ExportedProperty
    float mPivotX = 0f;

    /**
     * The y location of the point around which the view is rotated and scaled.
     */
    @ViewDebug.ExportedProperty
    float mPivotY = 0f;

    /**
     * The opacity of the View. This is a value from 0 to 1, where 0 means
     * completely transparent and 1 means completely opaque.
     */
    @ViewDebug.ExportedProperty
    float mAlpha = 1f;

    /**
     * The opacity of the view as manipulated by the Fade transition. This is a hidden
     * property only used by transitions, which is composited with the other alpha
     * values to calculate the final visual alpha value.
     */
    float mTransitionAlpha = 1f;
}
7、ListenerInfo封装了多个监听器的静态内部类。

static class ListenerInfo {
    /**
     * Listener used to dispatch focus change events.
     * This field should be made private, so it is hidden from the SDK.
     * {@hide}
     */
    protected OnFocusChangeListener mOnFocusChangeListener;

    /**
     * Listeners for layout change events.
     */
    private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;

    /**
     * Listeners for attach events.
     */
    private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;

    /**
     * Listener used to dispatch click events.
     * This field should be made private, so it is hidden from the SDK.
     * {@hide}
     */
    public OnClickListener mOnClickListener;

    /**
     * Listener used to dispatch long click events.
     * This field should be made private, so it is hidden from the SDK.
     * {@hide}
     */
    protected OnLongClickListener mOnLongClickListener;

    /**
     * Listener used to build the context menu.
     * This field should be made private, so it is hidden from the SDK.
     * {@hide}
     */
    protected OnCreateContextMenuListener mOnCreateContextMenuListener;

    private OnKeyListener mOnKeyListener;

    private OnTouchListener mOnTouchListener;

    private OnHoverListener mOnHoverListener;

    private OnGenericMotionListener mOnGenericMotionListener;

    private OnDragListener mOnDragListener;

    private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;

    OnApplyWindowInsetsListener mOnApplyWindowInsetsListener;
}





分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP