本章介绍Android应用开发中回去分辨率的方法及屏幕尺寸的计算方法。
一、通过Display class 获取到屏幕分辨率的接口
参考googlehttps://developer.android.com/reference/android/view/Display
注意:其中有很多接口已经标记deprecated弃用
Public methods | |
int | getWidth() This method was deprecated in API level 15. Use WindowMetrics#getBounds instead. |
int | This method was deprecated in API level 15. Use WindowMetrics#getBounds() instead. |
void | getCurrentSizeRange(Point outSmallestSize, Point outLargestSize) Return the range of display sizes an application can expect to encounter under normal operation, as long as there is no physical change in screen size. |
void | getRealMetrics(DisplayMetrics outMetrics) This method was deprecated in API level 31. Use WindowManager#getCurrentWindowMetrics() to identify the current size of the activity window. UI-related work, such as choosing UI layouts, should rely upon WindowMetrics#getBounds(). Use Configuration#densityDpi to get the current density. |
void | getMetrics(DisplayMetrics outMetrics) This method was deprecated in API level 30. Use WindowMetrics#getBounds() to get the dimensions of the application window. Use WindowMetrics#getDensity() to get the density of the application window. |
void | getRealSize(Point outSize) This method was deprecated in API level 31. Use WindowManager#getCurrentWindowMetrics() to identify the current size of the activity window. UI-related work, such as choosing UI layouts, should rely upon WindowMetrics#getBounds(). |
void | This method was deprecated in API level 30. Use WindowMetrics instead. Obtain a WindowMetrics instance by calling WindowManager#getCurrentWindowMetrics(), then call WindowMetrics#getBounds() to get the dimensions of the application window. |
void | getRectSize(Rect outSize) This method was deprecated in API level 30. Use WindowMetrics#getBounds() to get the dimensions of the application window. |
getMode() Returns the active mode of the display. | |
Gets the supported modes of this display. |
//如果在active中可以直接使用getWindow().getWindowManager()
WindowManager windowManager = getWindow().getWindowManager();
//如果是在在Service等中获取需要通过Context
WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Log.i(TAG, "getWidth:" + display.getWidth());
Log.i(TAG, "getHeight:" + display.getHeight());//不包含状态栏和导航栏
Point outSmallestSize = new Point();
Point outLargestSize = new Point();
display.getCurrentSizeRange(outSmallestSize, outLargestSize);
Log.i(TAG, "outSmallestSize:" + outSmallestSize.toString());
Log.i(TAG, "outLargestSize:" + outLargestSize.toString());
outLargestSize.x, outLargestSize.y
DisplayMetrics realDisplayMetrics = new DisplayMetrics();
display.getRealMetrics(realDisplayMetrics);//屏幕实际分辨率
Log.i(TAG, "getRealMetrics:" + realDisplayMetrics.toString());
// displayMetrics.widthPixels, displayMetrics.heightPixels
DisplayMetrics displayMetrics = new DisplayMetrics();
display.getMetrics(displayMetrics);//不包含状态栏和导航栏
Point outSize = new Point();
display.getRealSize(outSize);//屏幕实际分辨率
Log.i(TAG, "getRealSize:" + outSize.toString());
// outSize.x, outSize.y
display.getSize(outSize);//不包含状态栏和导航栏
Log.i(TAG, "getSize:" + outSize.toString());
Rect outRect = new Rect();
display.getRectSize(outRect);
Log.i(TAG, "getRectSize:" + outRect.toString());
//outRect.width(), outRect.height()
Display.Mode activeMode = display.getMode();
Log.i(TAG, "activeMode:" + activeMode.toString());
Display.Mode[] modes = display.getSupportedModes();
for (int i = 0; i < modes.length; i++) {
Log.i(TAG, "Mode[" + i + "]:"+ modes[i].toString());
}
我使用小米MIX4测试如下(MIX4屏幕的分辨率是1080 x 2400):
getWidth:1080
getHeight:2252
outSmallestSize:Point(1080, 932)
outLargestSize:Point(2296, 2252)
getRealMetrics:DisplayMetrics{density=2.75, width=1080, height=2400, scaledDensity=2.75, xdpi=394.705, ydpi=394.563}
getRealSize:Point(1080, 2400)
getSize:Point(1080, 2252)
getRectSize:Rect(0, 0 - 1080, 2252)
activeMode:{id=2, width=1080, height=2400, fps=120.00001, alternativeRefreshRates=[60.000004, 90.0]}
Mode[0]:{id=1, width=1080, height=2400, fps=60.000004, alternativeRefreshRates=[90.0, 120.00001]}
Mode[1]:{id=2, width=1080, height=2400, fps=120.00001, alternativeRefreshRates=[60.000004, 90.0]}
Mode[2]:{id=3, width=1080, height=2400, fps=90.0, alternativeRefreshRates=[60.000004, 120.00001]}
二、getResources().getDisplayMetrics()
//不包含状态栏和导航栏
DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics();
Log.i(TAG, "getDisplayMetrics:" + displayMetrics.toString());
Log.i(TAG, "widthPixels:" + displayMetrics.widthPixels + ", heightPixels:" + displayMetrics.heightPixels);
我使用小米MIX4测试如下(MIX4屏幕的分辨率是1080 x 2400):
getDisplayMetrics:DisplayMetrics{density=2.75, width=1080, height=2252, scaledDensity=2.75, xdpi=394.705, ydpi=394.563}
widthPixels:1080, heightPixels:2252
三、WindowMetrics#getBounds()
WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
WindowMetrics windowMetrics = windowManager.getCurrentWindowMetrics();
Rect rect = windowMetrics.getBounds();
Log.i(TAG, "rect:"+ rect.toString());
Log.i(TAG, "rect:"+ rect.toString());
我使用小米MIX4测试如下(MIX4屏幕的分辨率是1080 x 2400):
rect:Rect(0, 0 - 1080, 2400)
width:1080, height:2400
四、屏幕尺寸计算公式
屏幕尺寸计算公式:对角线 √((x)^2+(y)^2)

屏幕尺寸通常说的是英寸,1英寸 = 2.54厘米 = 25.4毫米
通过widthPixels=1080, heightPixels=2252, xdpi=394.705, ydpi=394.563 ,我们就可以计算屏幕尺寸
xdpi = (FLOAT(widthPixels) * 25.4f) / FLOAT(mm_width); // x_dpi 的计算公式
ydpi = (FLOAT(heightPixels) * 25.4f) / FLOAT(mm_height); // y_dpi 的计算公式
上面公式中mm_width和mm_height是屏幕的实际尺寸(单位毫米mm)
如果使用英寸为单位,公式为:
xdpi = FLOAT(widthPixels) / FLOAT(in_width); // x_dpi 的计算公式
ydpi = FLOAT(heightPixels) / FLOAT(in_height); // y_dpi 的计算公式
上面公式中in_width和in_width是屏幕的实际尺寸(单位英寸in)
in_width = widthPixels/xdpi
in_height = heightPixels/ydpi
屏幕尺寸 = √((widthPixels/xdpi)^2+(heightPixels/ydpi)^2)
使用代码:
Display display = getWindow().getWindowManager().getDefaultDisplay();
DisplayMetrics realDisplayMetrics = new DisplayMetrics();
display.getRealMetrics(realDisplayMetrics);
double screenSize = Math.sqrt(Math.pow(realDisplayMetrics.widthPixels/realDisplayMetrics.xdpi,2) + Math.pow(realDisplayMetrics.heightPixels/realDisplayMetrics.ydpi,2));
Log.i(TAG, "screenSize:" + screenSize);
打印
screenSize:6.669774043272333
与手机设置页面对比

五、源码
百度网盘链接:百度网盘 请输入提取码 提取码:test
github下载地址:
GitHub - liuzhengliang1102/AndroidStudio-LearnAppDevelopment
DisplayClassTest目录