【Android】获取状态栏高度、导航栏高度、全面屏尺寸的方法总结 | 您所在的位置:网站首页 › shell命令操作安卓状态栏 › 【Android】获取状态栏高度、导航栏高度、全面屏尺寸的方法总结 |
1. 获取屏幕高度
/**
* Get screen height
* The first one is to read the heightPixels parameter of DisplayMetrics
*/
private fun getScreenHeight(context: Context): Int {
return context.resources?.displayMetrics?.heightPixels ?: 0
}
/**
* Get screen Real height
* The second type is to read the defaultDisplay parameter in windowManager
*/
@Volatile
private var sRealSizes = arrayOfNulls(2)
private fun getScreenRealHeight(context: Context): Int {
var orientation = context.resources?.configuration?.orientation
orientation = if (orientation == 1) 0 else 1
if (sRealSizes[orientation] == null) {
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val display = windowManager.defaultDisplay
val point = Point()
display.getRealSize(point)
sRealSizes[orientation] = point
}
return sRealSizes[orientation]?.y ?: getScreenRealHeight(context)
}
复制代码
当我们使用上述代码获取屏幕高度时,会发现在不同case下获取高度不同: case1:非全面屏带有navigation bar收集屏幕高度1920,但是由于navitaionbar的存在,实际有效区域的高度只有1794 case2:非全面屏不带有navigation bar没有navigationbar时,获取的实际有效高度也是1920 case3:全面屏全面屏下,即使没有navigationbar,有效高度与实际高度依然不等,所以全面屏中尽量使用getScreenRealHeight获取屏幕高度 结论综上,各种case下获取屏幕高度方法 if (DeviceUtils.isAllScreenDevice()) { // The full screen needs to get the height through this method screenHeight = DisplayUtils.getScreenRealHeight(getContext()); } else { screenHeight = DisplayUtils.getScreenHeight(getContext()); } 复制代码 2. 获取屏幕宽度 private fun getScreenWidth(context: Context): Int { return context.resources?.displayMetrics?.widthPixels ?: 0 } private fun getScreenRealWidth(context: Context): Int { var orientation = context.resources?.configuration?.orientation orientation = if (orientation == 1) 0 else 1 if (sRealSizes[orientation] == null) { val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager val display = windowManager.defaultDisplay val point = Point() display.getRealSize(point) sRealSizes[orientation] = point } return sRealSizes[orientation]?.x ?: getScreenWidth(context) } 复制代码 3. 状态栏(status bar)高度 private fun getStatusBarHeight(window: Window, context: Context): Int { val localRect = Rect() window.decorView.getWindowVisibleDisplayFrame(localRect) var mStatusBarHeight = localRect.top if (0 == mStatusBarHeight) { try { val localClass = Class.forName("com.android.internal.R\$dimen") val localObject = localClass.newInstance() val i5 = localClass.getField("status_bar_height")[localObject].toString().toInt() mStatusBarHeight = context.resources.getDimensionPixelSize(i5) } catch (var6: ClassNotFoundException) { var6.printStackTrace() } catch (var7: IllegalAccessException) { var7.printStackTrace() } catch (var8: InstantiationException) { var8.printStackTrace() } catch (var9: NumberFormatException) { var9.printStackTrace() } catch (var10: IllegalArgumentException) { var10.printStackTrace() } catch (var11: SecurityException) { var11.printStackTrace() } catch (var12: NoSuchFieldException) { var12.printStackTrace() } } if (0 == mStatusBarHeight) { val resourceId: Int = context.resources.getIdentifier("status_bar_height", "dimen", "android") if (resourceId > 0) { mStatusBarHeight = context.resources.getDimensionPixelSize(resourceId) } } return mStatusBarHeight } 复制代码 4. 导航栏(gavigation bar)高度 private fun getNavigationBarHeight(context: Context): Int { val rid: Int = context.resources.getIdentifier("config_showNavigationBar", "bool", "android") return if (rid != 0) { val resourceId: Int = context.resources.getIdentifier("navigation_bar_height", "dimen", "android") context.resources.getDimensionPixelSize(resourceId) } else { 0 } } 复制代码 5. 判断是否全面屏 private fun isAllScreenDevice(context: Context): Boolean { if (VERSION.SDK_INT < 21) { return false } else { val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager val display = windowManager.defaultDisplay val point = Point() display.getRealSize(point) val width: Float val height: Float if (point.x < point.y) { width = point.x.toFloat() height = point.y.toFloat() } else { width = point.y.toFloat() height = point.x.toFloat() } if (height / width >= 1.97f) { return true } return false } } 复制代码 |
今日新闻 |
推荐新闻 |
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 |