1. 获得屏幕的宽高
private void getWindowHeight() {
int heightPixels = getResources().getDisplayMetrics().heightPixels;
int height = getWindowManager().getDefaultDisplay().getHeight();
Log.e(TAG, "heightPixels = "+heightPixels);
// output = 1280
Log.e(TAG, "height = "+height );
// output = 1280
}
2. 动态测量--获得控件在屏幕的坐标,可视区域
观察View的绘制流程,设置OnGlobalLayoutListener监听,布局完成时会调用onGlobalLayout(),在onGlobalLayout()方法里面获取空间的宽和高
// 测量树---------
mBtnLocation.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// 控件相对于手机屏幕的位置坐标
// 一个控件在整个屏幕中的坐标位置 --(状态栏48px)
int[] location1 = new int[2];
mBtnLocation.getLocationOnScreen(location1);
Log.e(TAG, "getLocationOnScreen : x = "+location1[0]+" y = "+location1[1] );