1. Obtenha a altura da tela
/**
* 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)
}
Quando usamos o código acima para obter a altura da tela, descobriremos que a altura é diferente em casos diferentes:
caso1: tela não inteira com barra de navegação
A altura da tela coletada é 1920, mas devido à navitaionbar
existência, a altura da área efetiva real é apenas 1794
caso2: tela não inteira sem barra de navegação
Quando não há barra de navegação, a altura efetiva real obtida também é 1920
caso 3: tela inteira
Na tela inteira, mesmo se não houver barra de navegação, a altura efetiva e a altura real ainda são diferentes, então tente usar a tela inteira para getScreenRealHeight
obter a altura da tela
Em conclusão
Em resumo, como obter a altura da tela em vários casos
if (DeviceUtils.isAllScreenDevice()) {
// The full screen needs to get the height through this method
screenHeight = DisplayUtils.getScreenRealHeight(getContext());
} else {
screenHeight = DisplayUtils.getScreenHeight(getContext());
}
2. Obtenha a largura da tela
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. A altura da barra de status
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. A altura da barra de navegação
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 se a tela inteira
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
}
}