Browse Source

feat: DisplayUtil增加底部导航栏方法

DoggyZhang 3 months ago
parent
commit
18e8352f00

+ 1 - 1
frame/bom/build.gradle

@@ -6,7 +6,7 @@ plugins {
 ext {
     GROUP_ID = 'com.wenext.android'
     ARTIFACT_ID = 'frame-bom'
-    VERSION = '6.1.3'
+    VERSION = '6.1.4'
 }
 
 dependencies {

+ 1 - 1
frame/util/build.gradle

@@ -7,7 +7,7 @@ plugins {
 ext {
     GROUP_ID = 'com.wenext.android'
     ARTIFACT_ID = 'frame-util'
-    VERSION = '6.0.3'
+    VERSION = '6.0.4'
 }
 
 if (project.FRAME_DEBUG != "true") {

+ 78 - 9
frame/util/src/main/java/com/adealink/frame/util/DisplayUtil.kt

@@ -8,11 +8,18 @@ import android.graphics.Point
 import android.graphics.Rect
 import android.util.DisplayMetrics
 import android.util.TypedValue
+import android.view.KeyCharacterMap
+import android.view.KeyEvent
+import android.view.ViewConfiguration
 import android.view.Window
 import android.view.WindowManager
 
 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 LANDSCAPE = 1
 
@@ -68,42 +75,56 @@ object DisplayUtil {
 
     @JvmStatic
     fun getStatusBarHeight(window: Window): Int {
+        if (sStatusBarHeight != -1) {
+            return sStatusBarHeight
+        }
+
         val localRect = Rect()
         window.decorView.getWindowVisibleDisplayFrame(localRect)
-        var mStatusBarHeight = localRect.top
-        if (0 == mStatusBarHeight) {
+        var statusBarHeight = localRect.top
+        if (0 == statusBarHeight) {
             try {
                 val c = Class.forName("com.android.internal.R\$dimen")
                 val o = c.newInstance()
                 val field = c.getField("status_bar_height")[o].toString().toInt()
-                mStatusBarHeight = getResources().getDimensionPixelSize(field)
+                statusBarHeight = getResources().getDimensionPixelSize(field)
             } catch (e: Exception) {
                 e.printStackTrace()
             }
         }
-        if (0 == mStatusBarHeight) {
+        if (0 == statusBarHeight) {
             val resourceId: Int = getResources().getIdentifier(
                 "status_bar_height",
                 "dimen",
                 "android"
             )
             if (resourceId > 0) {
-                mStatusBarHeight = getResources().getDimensionPixelSize(resourceId)
+                statusBarHeight = getResources().getDimensionPixelSize(resourceId)
             }
         }
-        return mStatusBarHeight
+        return statusBarHeight.also {
+            sStatusBarHeight = it
+        }
     }
 
     @JvmStatic
     fun getActionBarHeight(context: Context): Int {
+        if (sActionBarHeight != -1) {
+            return sActionBarHeight
+        }
         val tv = TypedValue()
-        return if (context.theme.resolveAttribute(16843499,
+        val actionBarHeight = if (context.theme.resolveAttribute(
+                16843499,
                 tv,
-                true)
+                true
+            )
         ) TypedValue.complexToDimensionPixelSize(
             tv.data,
             context.resources.displayMetrics
         ) else 0
+        return actionBarHeight.also {
+            sActionBarHeight = it
+        }
     }
 
     @JvmStatic
@@ -116,7 +137,7 @@ object DisplayUtil {
         return getResources().configuration.orientation == 1
     }
 
-    fun getOrientation() : Int {
+    fun getOrientation(): Int {
         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
+    }
+
+
 }