1. Obtain the screen height
/** * Get screen height * The first one is to read the heightPixels parameter of DisplayMetrics */
private fun getScreenHeight(context: Context): Int {
returncontext.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 {
varorientation = 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
}
returnsRealSizes[orientation]? .y ? : getScreenRealHeight(context) }Copy the code
When we use the above code to get the screen height, we will find that the height is different in different cases:
Case1: non-full screen with navigation bar
The collection screen height is 1920, but due to navitaionbar, the actual effective area is only 1794 high
Case2: Non-full screen without navigation bar
Without navigationBar, the actual effective height is also 1920
Case3: Full screen
In full screen, even without navigationBar, the effective height and the actual height are still different, so try to use getScreenRealHeight in full screen to get the screen height
conclusion
To sum up, various cases under the screen height method
if (DeviceUtils.isAllScreenDevice()) {
// The full screen needs to get the height through this method
screenHeight = DisplayUtils.getScreenRealHeight(getContext());
} else {
screenHeight = DisplayUtils.getScreenHeight(getContext());
}
Copy the code
2. Obtain the screen width
private fun getScreenWidth(context: Context): Int {
returncontext.resources? .displayMetrics? .widthPixels ? :0
}
private fun getScreenRealWidth(context: Context): Int {
varorientation = 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
}
returnsRealSizes[orientation]? .x ? : getScreenWidth(context) }Copy the code
3. Status bar height
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
}
Copy the code
4. Gavigation Bar height
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}}Copy the code
5. Check whether the screen is full
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.97 f) {
return true
}
return false}}Copy the code