1. Get screen height
/**
* 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<Point>(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)
}
When we use the above code to get the height of the screen, we will find that the height is different in different cases:
case1: non-full screen with navigation bar
The height of the collected screen is 1920, but due to navitaionbar
the existence, the height of the actual effective area is only 1794
case2: non-full screen without navigation bar
When there is no navigationbar, the actual effective height obtained is also 1920
case3: full screen
Under the full screen, even if there is no navigationbar, the effective height is still different from the actual height, so try to use the full screen to getScreenRealHeight
get the screen height
in conclusion
In summary, how to get the screen height in various cases
if (DeviceUtils.isAllScreenDevice()) {
// The full screen needs to get the height through this method
screenHeight = DisplayUtils.getScreenRealHeight(getContext());
} else {
screenHeight = DisplayUtils.getScreenHeight(getContext());
}
2. Get the screen width
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. The height of the 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. The height of the navigation 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. Determine whether the full screen
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
}
}