最近学习了一下View的坐标系,在此记录一下。
其实感觉看懂了下面这张图,也就基本可以理解View的坐标系了。

屏幕的左上角为(0, 0)
View的几个方法
-
getLeft()
是view自身的左边到其父布局左边的距离,一定记住是距父布局而不是距屏幕,其他的几个方法类似 -
getTop()
是view自身的顶边到其父布局顶边的距离 -
getRight()
是view自身的右边到其父布局左边的距离 -
getBottom()
是view自身的底边到其父布局顶边的距离 -
getHeight()
获取控件自身的高度 -
getWidth()
获取控件自身的宽度
/**
* Return the width of the your 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.
*
* @return The height of your view, in pixels.
*/
@ViewDebug.ExportedProperty(category = "layout")
public final int getHeight() {
return mBottom - mTop;
}
其实查看了View的源码不难发现,实际上宽度是通过计算mRight - mLeft
得到的,而高度则是mBottom - mTop
得到的
MotionEvent的几个方法
-
getX()
获取点击事件相对控件自己左边的x轴坐标,即点击事件距离控件自己左边的距离 -
getY()
获取点击事件相对控件自己顶边的y轴坐标,即点击事件距离控件自己顶边的距离 -
getRawX()
获取点击事件相对整个屏幕左边的x轴坐标,即点击事件距离整个屏幕左边的距离 -
getRawY()
获取点击事件相对整个屏幕顶边的y轴坐标,即点击事件距离整个屏幕顶边的距离
大概就是这些,以后再补充。
今年要多学习,并且养成记录和写博客的好习惯!坚持!
网友评论