坐标系详解

前言

Android系统中有两种坐标系,分别为Android坐标系和View坐标系。了解这两种坐标系能够帮助我们实现View的各种操作,更好的控制View.


1.Android坐标系

        在Android中,屏幕左上角的顶点作为Android坐标原点(0,0),X轴向右是正方向,向下是Y轴正方向,如下图:


2.View坐标系

        View坐标系与Android坐标系两者共同存在,他们一起存在可以更好的帮助开发者更好的控制view。如下图可以帮助我们更好的分析view坐标:




View获取自身宽高
  获取宽 获取高
方法一 width = getRight()-getLeft() height=getBottom()-getTop()
方法二 getWidth() getHeight()
以上两个方法获取的宽高 是一致的,我们从源码的角度分别看一下 getWidth()getHeight()方法,如下:


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



/** * Return the width of the your view. * 获取view的宽度 * @return The width of your view, in pixels. */@ViewDebug.ExportedProperty(category = "layout")public final int getWidth() { return mRight - mLeft;}


/**
 * Return the height of your view.
 *获取view高度
 * @return The height of your view, in pixels.
 */
@ViewDebug.ExportedProperty(category = "layout")
public final int getHeight() {
    return mBottom - mTop;
}

从上面方法我们可以看出这里是获取view的宽度,接着我们在具体看一下mRight与mLeft是什么?

/**
 * The distance in pixels from the left edge of this view's parent
 * to the left edge of this view.
 * {@hide}
 */
@ViewDebug.ExportedProperty(category = "layout")
protected int mLeft;
/**
 * The distance in pixels from the left edge of this view's parent
 * to the right edge of this view.
 * {@hide}
 */
@ViewDebug.ExportedProperty(category = "layout")
protected int mRight;


我们从注释上就可以看到 mLeft其实就是获取从父布局左侧边缘到控件(父布局里面的一个子控件)左侧边缘的一段距离。

mRight也是同样的道理,这里就不相信叙述了,自己看注释。

总结:

V iew 方法 作用
getTop() 获取View自身顶边到其父布局顶边的距离
getLeft() 获取View自身左边到其父布局左边的距离
getRight() 获取View自身右边到其父布局左边的距离
getBottom() 获取View自身底边到其父布局顶边的距离


MotionEvent提供的方法
方法
作用
getX() 获取点击事件距离控件左边的距离,即视图坐标
getY() 获取点击事件距离控件顶边的距离,即视图坐标
getRawX() 获取点击事件距离整个屏幕左边距离,即绝对坐标
getRawY() 获取点击事件距离整个屏幕顶边的的距离,即绝对坐标

eg:

getX()
/**
 * 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.
 */
@ViewDebug.ExportedProperty(category = "drawing")
public float getX() {
    return mLeft + getTranslationX();
}


/**
 * 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.
 */
@ViewDebug.ExportedProperty(category = "drawing")
public float getTranslationX() {
    return mRenderNode.getTranslationX();
}


/**
 * Returns the translation value for this display list on the X axis, in pixels.
 *
 * @see #setTranslationX(float)
 */
public float getTranslationX() {
    return nGetTranslationX(mNativeRenderNode);
}

private static native float nGetTranslationX(long renderNode);//底层c处理



猜你喜欢

转载自blog.csdn.net/u014133119/article/details/80715719