1、View的坐标
- /**
- *Toppositionofthisviewrelativetoitsparent.
- *相对应父控件的top位置,单位为像素,即头部到父控件的距离
- *@returnThetopofthisview,inpixels.
- */
- @ViewDebug.CapturedViewProperty
- publicfinalintgetTop(){
- returnmTop;
- }
/**
* Top position of this view relative to its parent.
*相对应父控件的top位置,单位为像素,即头部到父控件的距离
* @return The top of this view, in pixels.
*/
@ViewDebug.CapturedViewProperty
public final int getTop() {
return mTop;
}
其它的getBottom,getLeft,getRight类似
图例:

2、点击View时MotionEvent的坐标
getRawY:表示相对于屏幕左上角的y坐标值(注意:这个屏幕左上角是手机屏幕左上角,不管activity是否有titleBar或是否全屏幕),
getY:表示相对于View自身左上角的y坐标
图例:

组合图:

由上图可知:
View到屏幕上边的距离=e.getRawY-.getY
Item到屏幕上边的距离=e.getRawY-e.getY+item.getTop
等等
本文详细解析了Android中View的坐标系统及如何通过MotionEvent获取点击位置,结合实例图解,深入理解布局与屏幕之间的相对位置关系。
1428

被折叠的 条评论
为什么被折叠?



