在 Android开发中经常会遇到获取屏幕高度做布局适配的情况,那么今天就来讲讲我在项目中遇到的一个坑。
问题的发现
在Android 10 全面屏手机的横屏全屏中,项目中的视频播放时,在底部留了一段虚拟导航栏的高度的空白,起初我以为是全屏代码是不是有问题导致,全屏代码如下:
public static void hideSystemUI(Activity activity) {
if (activity == null) {
return;
}
View decorView = activity.getWindow().getDecorView();
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
int visibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
visibility |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
}
decorView.setSystemUiVisibility(visibility);
}
这段代码是谷歌官方文档里提取出来的,我用新建一个项目的方式运行,发现没有问题,深入业务逻辑中发现播放器的 View 是自动计算大小赋值的。
public static int getScreenHeight(Context context) {
DisplayMetrics dm = new DisplayMetrics();
WindowManager localWindowManager =
(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
localWindowManager.getDefaultDisplay().getMetrics(dm);
return dm.heightPixels;
}
上面的代码乍看没什么问题,很多同学就是用这段代码获取高度的,深入 getMetrics() 方法,看到文档的注释心里有数了。
/**
* Gets display metrics that describe the size and density of this display.
* The size returned by this method does not necessarily represent the
* actual raw size (native resolution) of the display.
* <p>
* 1. The returned size may be adjusted to exclude certain system decor elements
* that are always visible.
* </p><p>
* 2. It may be scaled to provide compatibility with older applications that
* were originally designed for smaller displays.
* </p><p>
* 3. It can be different depending on the WindowManager to which the display belongs.
* </p><p>
* - If requested from non-Activity context (e.g. Application context via
* {@code (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)})
* metrics will report the size of the entire display based on current rotation and with
* subtracted system decoration areas.
* </p><p>
* - If requested from activity (either using {@code getWindowManager()} or
* {@code (WindowManager) getSystemService(Context.WINDOW_SERVICE)}) resulting metrics will
* correspond to current app window metrics. In this case the size can be smaller than physical
* size in multi-window mode.
* </p>
*
* @param outMetrics A {@link DisplayMetrics} object to receive the metrics.
*/
public void getMetrics(DisplayMetrics outMetrics) {...}
// 和谐翻译
// 此方法获取的显示大小和密度指标不一定表示实际大小的指标。
// 1. 大小可能已经调整过,排除了一些系统永远可见的元素
// 2. 为了适配老的应用,可能已经被缩放了
// 3. 依赖不同的 WindowManager 可能是不同的值
看到这里聪明的你已经知道了,那留的那一块就是这个方法导致的。
解决问题
根据同级 api 找到了以下的方法
/**
* Gets display metrics based on the real size of this display.
* <p>
* The size is adjusted based on the current rotation of the display.
* </p><p>
* The real size may be smaller than the physical size of the screen when the
* window manager is emulating a smaller display (using adb shell wm size).
* </p>
*
* @param outMetrics A {@link DisplayMetrics} object to receive the metrics.
*/
public void getRealMetrics(DisplayMetrics outMetrics) {...}
// 和谐翻译
// 获取真实尺寸的显示指标
// 尺寸会根据横竖屏调整
// 当 window manager模拟了一个小尺寸时,返回值可能会比物理尺寸小
显然这个方法更适合全屏隐藏状态栏及导航栏的情况, 更改代码后解决问题:
public static int getScreenHeight(Context context) {
DisplayMetrics dm = null;
try {
dm = new DisplayMetrics();
WindowManager localWindowManager =
(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
localWindowManager.getDefaultDisplay().getRealMetrics(dm);
} else {
localWindowManager.getDefaultDisplay().getMetrics(dm);
}
return dm.heightPixels;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
总结
使用系统方法时一定要读一下 api 文档说明,以免踩了不必要的坑。
代码搬运时先理解,后搬运,切忌。