美文网首页Android UI 适配
不同刘海屏幕获取安全高度

不同刘海屏幕获取安全高度

作者: ben大福 | 来源:发表于2019-07-10 15:13 被阅读0次

    系统获取大于等于9.0才有

    decorView.rootWindowInsets?.displayCutout.safeInsetTop  (kotlin)
    

    华为

    判断
    val cl: ClassLoader = context!!.classLoader
            val HwNotchSizeUtil: Class<*> = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil")
            val get = HwNotchSizeUtil.getMethod("hasNotchInScreen")
            ret = get.invoke(HwNotchSizeUtil) as Boolean
    
    获取
      val cl = context!!.classLoader
            val HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil")
            val get: Method = HwNotchSizeUtil.getMethod("getNotchSize")
            ret = get.invoke(HwNotchSizeUtil) as IntArray
    

    小米

    判断
    com.bilibili.droid.SystemProperties.get("ro.miui.notch", "0") == "1"
    
    获取
      val res = context!!.resources
        var resourceId = res.getIdentifier("notch_height", "dimen", "android");
        if (resourceId > 0) {
            return res.getDimensionPixelSize(resourceId);
        }
        resourceId = res.getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            return res.getDimensionPixelSize(resourceId);
        }
    

    小米有些版本可以通过notch_height获取,有些版本没有直接获取状态栏高度

    魅族

    判断
    var fringeDevice = false
        try {
            val clazz = Class.forName("flyme.config.FlymeFeature")
            val field = clazz.getDeclaredField("IS_FRINGE_DEVICE")
            fringeDevice = field.get(null) as Boolean
        } catch (e: Exception) {
        }
    
    
        if (fringeDevice) {
            // 判断隐藏刘海开关(默认关)
            val isFringeHidden = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                Settings.Global.getInt(window.getContext().getContentResolver(),
                        "mz_fringe_hide", 0) == 1
            } else {
                true //当做默认值
            }
            return !isFringeHidden
        }
    
        return false
    
    获取
    val res = context!!.resources
        var notchHeight = 0
        val fhid = res.getIdentifier("fringe_height", "dimen", "android")
        if (fhid > 0) {
            notchHeight = res.getDimensionPixelSize(fhid)
        }
    

    VIVO

    判断
            try {
            val cl =context.classLoader
            val vivoFtFeature = cl.loadClass("android.util.FtFeature")
            val get = vivoFtFeature.getMethod("isFeatureSupport", Int::class.javaPrimitiveType!!)
            //表示是否有凹槽
            val VIVO_HAS_NOTCH_DISPLAY:Int = 0x00000020
            return get.invoke(vivoFtFeature, VIVO_HAS_NOTCH_DISPLAY) as Boolean
        } catch (e: Exception) {
            return false
        }
    
    获取
     27dp
    

    OPPO

    判断
    return window.context.packageManager
                    .hasSystemFeature("com.oppo.feature.screen.heteromorphism");
    
    获取
      80
    

    oppo和vivo是默认的高度,没有厂商api

    相关文章

      网友评论

        本文标题:不同刘海屏幕获取安全高度

        本文链接:https://www.haomeiwen.com/subject/qedhkctx.html