测量相关的工具方法
public static int resolveSize(int size, int measureSpec) {
return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
}
/**
* @param size 想要的大小
* @param measureSpec: 父容器的MeasureSpec
* @param childMeasuredState : 子View的MeasureSpec
* return 新的 MeasureSpec
**/
public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
final int specMode = MeasureSpec.getMode(measureSpec);
final int specSize = MeasureSpec.getSize(measureSpec);
final int result;
switch (specMode) {
case MeasureSpec.AT_MOST:
if (specSize < size) {
result = specSize | MEASURED_STATE_TOO_SMALL;
} else {
result = size;
}
break;
case MeasureSpec.EXACTLY:
result = specSize;
break;
case MeasureSpec.UNSPECIFIED:
default:
result = size;
}
return result | (childMeasuredState & MEASURED_STATE_MASK);
}
获取位置相关方法
- getLocationInWindow(int[] pos): Computes the coordinates of this view in its window.
获取坐标相对于当前窗口 - getLocationOnScreen(int[] pos): Computes the coordinates of this view on the screen.
获取坐标相对于整个屏幕(手机屏幕) - getWindowVisibleDisplayFrame(Rect rect): 获取window可视区域大小:
程序显示的区域,包括标题栏,不包括状态栏; rect.top 为状态栏的高度;
返回示例 rect{0, 75, 1080, 1776}
参考:http://www.cnblogs.com/ai-developers/p/4413585.html
- getLocalVisibleRect(Rect rect): 获取view本身可见的坐标区域,坐标以自己的左上角为原点(0,0) ,如果view部分被遮挡,值会发生改变,
没有遮挡时的返回示例:rect {0,0, 90,840},即 90 为宽,840为高; - getGlobalVisibleRect(Rect rect) : 是获取view在屏幕坐标中的可视区域
getGlobalVisibleRect还可以接受第二个Point类型的参数:
targetView.getGlobalVisibleRect(Rect r, Point gobalOffset) - getHitRect(Rect rect): Hit rectangle in parent's coordinates
获取view所在矩阵,矩阵相对于父坐标;
网友评论