|
@@ -8,11 +8,18 @@ import android.graphics.Point
|
|
|
import android.graphics.Rect
|
|
import android.graphics.Rect
|
|
|
import android.util.DisplayMetrics
|
|
import android.util.DisplayMetrics
|
|
|
import android.util.TypedValue
|
|
import android.util.TypedValue
|
|
|
|
|
+import android.view.KeyCharacterMap
|
|
|
|
|
+import android.view.KeyEvent
|
|
|
|
|
+import android.view.ViewConfiguration
|
|
|
import android.view.Window
|
|
import android.view.Window
|
|
|
import android.view.WindowManager
|
|
import android.view.WindowManager
|
|
|
|
|
|
|
|
object DisplayUtil {
|
|
object DisplayUtil {
|
|
|
|
|
|
|
|
|
|
+ private var sStatusBarHeight: Int = -1
|
|
|
|
|
+ private var sNaviBarHeight: Int = -1
|
|
|
|
|
+ private var sActionBarHeight: Int = -1
|
|
|
|
|
+
|
|
|
private const val PORTRAIT = 0
|
|
private const val PORTRAIT = 0
|
|
|
private const val LANDSCAPE = 1
|
|
private const val LANDSCAPE = 1
|
|
|
|
|
|
|
@@ -68,42 +75,56 @@ object DisplayUtil {
|
|
|
|
|
|
|
|
@JvmStatic
|
|
@JvmStatic
|
|
|
fun getStatusBarHeight(window: Window): Int {
|
|
fun getStatusBarHeight(window: Window): Int {
|
|
|
|
|
+ if (sStatusBarHeight != -1) {
|
|
|
|
|
+ return sStatusBarHeight
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
val localRect = Rect()
|
|
val localRect = Rect()
|
|
|
window.decorView.getWindowVisibleDisplayFrame(localRect)
|
|
window.decorView.getWindowVisibleDisplayFrame(localRect)
|
|
|
- var mStatusBarHeight = localRect.top
|
|
|
|
|
- if (0 == mStatusBarHeight) {
|
|
|
|
|
|
|
+ var statusBarHeight = localRect.top
|
|
|
|
|
+ if (0 == statusBarHeight) {
|
|
|
try {
|
|
try {
|
|
|
val c = Class.forName("com.android.internal.R\$dimen")
|
|
val c = Class.forName("com.android.internal.R\$dimen")
|
|
|
val o = c.newInstance()
|
|
val o = c.newInstance()
|
|
|
val field = c.getField("status_bar_height")[o].toString().toInt()
|
|
val field = c.getField("status_bar_height")[o].toString().toInt()
|
|
|
- mStatusBarHeight = getResources().getDimensionPixelSize(field)
|
|
|
|
|
|
|
+ statusBarHeight = getResources().getDimensionPixelSize(field)
|
|
|
} catch (e: Exception) {
|
|
} catch (e: Exception) {
|
|
|
e.printStackTrace()
|
|
e.printStackTrace()
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- if (0 == mStatusBarHeight) {
|
|
|
|
|
|
|
+ if (0 == statusBarHeight) {
|
|
|
val resourceId: Int = getResources().getIdentifier(
|
|
val resourceId: Int = getResources().getIdentifier(
|
|
|
"status_bar_height",
|
|
"status_bar_height",
|
|
|
"dimen",
|
|
"dimen",
|
|
|
"android"
|
|
"android"
|
|
|
)
|
|
)
|
|
|
if (resourceId > 0) {
|
|
if (resourceId > 0) {
|
|
|
- mStatusBarHeight = getResources().getDimensionPixelSize(resourceId)
|
|
|
|
|
|
|
+ statusBarHeight = getResources().getDimensionPixelSize(resourceId)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- return mStatusBarHeight
|
|
|
|
|
|
|
+ return statusBarHeight.also {
|
|
|
|
|
+ sStatusBarHeight = it
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@JvmStatic
|
|
@JvmStatic
|
|
|
fun getActionBarHeight(context: Context): Int {
|
|
fun getActionBarHeight(context: Context): Int {
|
|
|
|
|
+ if (sActionBarHeight != -1) {
|
|
|
|
|
+ return sActionBarHeight
|
|
|
|
|
+ }
|
|
|
val tv = TypedValue()
|
|
val tv = TypedValue()
|
|
|
- return if (context.theme.resolveAttribute(16843499,
|
|
|
|
|
|
|
+ val actionBarHeight = if (context.theme.resolveAttribute(
|
|
|
|
|
+ 16843499,
|
|
|
tv,
|
|
tv,
|
|
|
- true)
|
|
|
|
|
|
|
+ true
|
|
|
|
|
+ )
|
|
|
) TypedValue.complexToDimensionPixelSize(
|
|
) TypedValue.complexToDimensionPixelSize(
|
|
|
tv.data,
|
|
tv.data,
|
|
|
context.resources.displayMetrics
|
|
context.resources.displayMetrics
|
|
|
) else 0
|
|
) else 0
|
|
|
|
|
+ return actionBarHeight.also {
|
|
|
|
|
+ sActionBarHeight = it
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@JvmStatic
|
|
@JvmStatic
|
|
@@ -116,7 +137,7 @@ object DisplayUtil {
|
|
|
return getResources().configuration.orientation == 1
|
|
return getResources().configuration.orientation == 1
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- fun getOrientation() : Int {
|
|
|
|
|
|
|
+ fun getOrientation(): Int {
|
|
|
return getConfiguration().orientation
|
|
return getConfiguration().orientation
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -196,4 +217,52 @@ object DisplayUtil {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取虚拟菜单的高度,若无则返回0
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param context
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ fun getNavMenuHeight(context: Context): Int {
|
|
|
|
|
+ if (!isNavMenuExist(context)) {
|
|
|
|
|
+ return 0
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (sNaviBarHeight != -1) {
|
|
|
|
|
+ return sNaviBarHeight
|
|
|
|
|
+ }
|
|
|
|
|
+ val resourceNavHeight: Int =
|
|
|
|
|
+ getResourceNavHeight(context)
|
|
|
|
|
+ if (resourceNavHeight >= 0) {
|
|
|
|
|
+ return resourceNavHeight.also {
|
|
|
|
|
+ sNaviBarHeight = resourceNavHeight
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 小米 MIX 有nav bar, 而 getRealScreenSize(context)[1] - getScreenHeight(context) = 0
|
|
|
|
|
+ return (getRealScreenSize(context).y - getScreenHeight()).also {
|
|
|
|
|
+ sNaviBarHeight = resourceNavHeight
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fun isNavMenuExist(context: Context): Boolean {
|
|
|
|
|
+ //通过判断设备是否有返回键、菜单键(不是虚拟键,是手机屏幕外的按键)来确定是否有navigation bar
|
|
|
|
|
+ val hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey()
|
|
|
|
|
+ val hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK)
|
|
|
|
|
+
|
|
|
|
|
+ // 做任何你需要做的,这个设备有一个导航栏
|
|
|
|
|
+ return !hasMenuKey && !hasBackKey
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private fun getResourceNavHeight(context: Context): Int {
|
|
|
|
|
+ // 小米4没有nav bar, 而 navigation_bar_height 有值
|
|
|
|
|
+ val resourceId =
|
|
|
|
|
+ context.getResources().getIdentifier("navigation_bar_height", "dimen", "android")
|
|
|
|
|
+ if (resourceId > 0) {
|
|
|
|
|
+ return context.getResources().getDimensionPixelSize(resourceId)
|
|
|
|
|
+ }
|
|
|
|
|
+ return -1
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
}
|
|
}
|