Android上获取屏幕长宽、控件的位置时不时就会用到,每次都要把所有API运行下看下那个是想要的,太没效率,这里总结下常用的获取方式。
获取屏幕的长宽是系统的固有的,怎么都不会改变的,主要是要获取到在屏幕上的status bar Activity 中的titil以及实际的decorView的位置和内部的控件的位置,这里要注意的一点是Activity一般是占满整个屏幕的所以在Activity上的坐标就是在Screen的坐标。
获取屏幕上的一些参数的API
WindowManager wm=getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics=new DiaplayMetrics();
wm.getDefaultDisplay().getMetrics()
// 屏幕宽度px
screenWidth = metrics.widthPixels;
// 屏幕长度px
screenHeight = metrics.heightPixels;
// 屏幕相对像素密度比值 如 0.5:1:2:3
screendesity = metrics.density;
// 屏幕相对密度 如:160dpi
screendesityDIP = metrics.densityDpi;
获取状态栏、标题栏、DecorView的坐标
Window win=getWindow();
Rect rect=new Rect();
// 1.获取到decorView的显示位置,但是必须要在decorView渲染出来时才能获取到有效值,需要延迟一段时间或者在onWindowFocusChanged中获取
win.getDecorView().getWindowVisibleDisplayFrame(rect)
statusBarHeight= rect.getTop();
contentViewTop=win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
titleBarHeight=contentViewTop-statusBareHegith;
// 2.因为系统的状态栏在系统中一般是固定的高度,它会放在系统的资源文件中,获取资源id以后再获取到对应的px值,这样获取状态栏的高度是可靠的
int resourcesId = getResources().getIdentifier("status_bar_height","dimen","android");
statusBarHeight=getResources().getDimensionPixeSize(resourcesId )
获取View在屏幕中的位置或者在父控件中的位置
int[] location =new new int[2];
// 获取在本Window的坐标(Activity)一般和screen一样
tv1.getLocationInWindow(location);
// 获取在整个Screen的坐标
tv1.getLocationOnScreen(location);
// 一下API都是相对父布局的
// 左边的x坐标
tv1.getLeft();
// 上边的y坐标
tv1.getTop();
// 右边的x坐标
tv1.getRight();
// 下边的要z坐标
tv1.getBottom();
代码
private void refreshViewPos() {
int[] location = new int[2];
int[] location2 = new int[2];
tv1.getLocationOnScreen(location);
Log.d(TAG, "tv1 getLocationOnScreen X " + location[0]);
Log.d(TAG, "tv1 getLocationOnScreen Y " + location[1]);
tv1.getLocationInWindow(location2);
Log.d(TAG, "tv1 getLocationInWindow X" + location2[0]);
Log.d(TAG, "tv1 getLocationInWindow Y" + location2[1]);
Log.d(TAG, "tv1 getX " + tv1.getX());
Log.d(TAG, "tv1 getY " + tv1.getY());
rl1.getLocationOnScreen(location);
Log.d(TAG, "rl1 getLocationOnScreen X " + location[0]);
Log.d(TAG, "rl1 getLocationOnScreen Y " + location[1]);
rl1.getLocationInWindow(location2);
Log.d(TAG, "rl1 getLocationInWindow X" + location2[0]);
Log.d(TAG, "rl1 getLocationInWindow Y" + location2[1]);
tv2.getLocationOnScreen(location);
Log.d(TAG, "tv2 getLocationOnScreen X " + location[0]);
Log.d(TAG, "tv2 getLocationOnScreen Y " + location[1]);
tv2.getLocationInWindow(location2);
Log.d(TAG, "tv2 getLocationInWindow X" + location[0]);
Log.d(TAG, "tv2 getLocationInWindow Y" + location2[1]);
Log.d(TAG, "tv2 getX " + tv2.getX());
Log.d(TAG, "tv2 getY " + tv2.getY());
Log.d(TAG, "tv2 getTop " + tv2.getTop());
Log.d(TAG, "tv2 getLeft " + tv2.getLeft());
Log.d(TAG, "tv2 getRight " + tv2.getRight());
Log.d(TAG, "tv2 getBottom " + tv2.getBottom());
rl2.getLocationOnScreen(location);
Log.d(TAG, "rl2 getLocationOnScreen X " + location[0]);
Log.d(TAG, "rl2 getLocationOnScreen Y " + location[1]);
rl2.getLocationInWindow(location2);
Log.d(TAG, "rl2 getLocationInWindow X" + location2[0]);
Log.d(TAG, "rl2 getLocationInWindow Y" + location2[1]);
tv3.getLocationOnScreen(location);
Log.d(TAG, "tv3 getLocationOnScreen X " + location[0]);
Log.d(TAG, "tv3 getLocationOnScreen Y " + location[1]);
tv3.getLocationInWindow(location2);
Log.d(TAG, "tv3 getLocationInWindow X" + location2[0]);
Log.d(TAG, "tv3 getLocationInWindow Y" + location2[1]);
Log.d(TAG, "tv3 getX " + tv3.getX());
Log.d(TAG, "tv3 getY " + tv3.getY());
}
/**
* 原理是根据decorView来获取高度,但是decorView未绘制时获取的为0
*/
private void refreshScreenControllLength() {
// 获取到decorView在屏幕中的位置
Window win = getWindow();
win.getDecorView().getWindowVisibleDisplayFrame(mRect);
statusBarHeight = mRect.top;
contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
titleBarHeight = contentViewTop - statusBarHeight;
Log.d(TAG, "statusBarHeight " + statusBarHeight);
Log.d(TAG, "contentViewTop " + contentViewTop);
Log.d(TAG, "titleBarHeight " + titleBarHeight);
// 03-31 11:48:25.153: D/MainActivity(18702): statusBarHeight 60
// 03-31 11:48:25.153: D/MainActivity(18702): contentViewTop 204
// 03-31 11:48:25.153: D/MainActivity(18702): titleBarHeight 144
}
private void refreshScreenControllLength2() {
// 获取到decorView在屏幕中的位置
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
statusBarHeight = getResources().getDimensionPixelSize(resourceId);
Log.d(TAG, "statusBarHeight " + statusBarHeight);
// Log.d(TAG, "contentViewTop " +contentViewTop);
// Log.d(TAG, "titleBarHeight " +titleBarHeight);
// 03-31 11:48:24.848: D/MainActivity(18702): statusBarHeight 60
}
private void refreshScreenInfoData() {
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;
screendesity = metrics.density;
screendesityDIP = metrics.densityDpi;
Log.d(TAG, "screenWidth " + screenWidth);
Log.d(TAG, "screenHeight " + screenHeight);
Log.d(TAG, "screendesity " + screendesity);
Log.d(TAG, "screendesityDIP " + screendesityDIP);
// 03-31 11:48:24.853: D/MainActivity(18702): screenWidth 1080
// 03-31 11:48:24.853: D/MainActivity(18702): screenHeight 1920
// 03-31 11:48:24.853: D/MainActivity(18702): screendesity 3.0
// 03-31 11:48:24.853: D/MainActivity(18702): screendesityDIP 480
}