Android刘海屏的适配

2020-05-26  本文已影响0人  code希必地

这里主要是介绍一下Android P中刘海屏的适配以及Android P之前的适配。为什么要分开呢?因为Android P之前官方还没提供API来进行适配,都是由各家厂商来提供适配方案的。

1、Android P的适配

1.1、 DisplayCutout类

我们可以通过这个类来确定非功能区域的位置和形状,这些区域不应该展示内容。 要确定这些凹口屏幕区域是否存在及其位置,请使用 getDisplayCutout() 函数。
先看下这个类中的主要方法:

private void adapterNotchScreen() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            final View decorView = getWindow().getDecorView();

            decorView.post(new Runnable() {
                @Override
                public void run() {
                    WindowInsets rootWindowInsets = decorView.getRootWindowInsets();
                    if (rootWindowInsets != null) {
                        DisplayCutout displayCutout = rootWindowInsets.getDisplayCutout();
                        // 当全屏顶部显示黑边时,getDisplayCutout()返回为null
                        if (displayCutout == null)
                            return;
                        Log.e("TAG", "安全区域距离屏幕左边的距离 SafeInsetLeft:" + displayCutout.getSafeInsetLeft());
                        Log.e("TAG", "安全区域距离屏幕右部的距离 SafeInsetRight:" + displayCutout.getSafeInsetRight());
                        Log.e("TAG", "安全区域距离屏幕顶部的距离 SafeInsetTop:" + displayCutout.getSafeInsetTop());
                        Log.e("TAG", "安全区域距离屏幕底部的距离 SafeInsetBottom:" + displayCutout.getSafeInsetBottom());
                        // 获得刘海区域
                        List<Rect> rects = displayCutout.getBoundingRects();
                        if (rects == null || rects.size() == 0) {
                            Log.e("TAG", "不是刘海屏");
                        } else {
                            Log.e("TAG", "刘海屏数量:" + rects.size());
                            for (Rect rect : rects) {
                                Log.e("TAG", "刘海屏区域:" + rect);
                            }
                        }
//                        tv_timer.setTranslationY(displayCutout.getSafeInsetTop());
                    }
                }
            });
        }
    }

注意:在适配之前,状态栏区域为黑色,此时通过getDisplayCutout()获取的DisplayCutout对象为null,故这个类的使用应该是在适配完成之后。

1.2、设置凹口屏幕的显示模式

使用例子:

WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
    getWindow().setAttributes(lp);

Android P中增加了一个新的属性layoutInDisplayCutoutMode ,其中包含了三种模式。

public class NotchActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //开局就一张背景图
        setContentView(R.layout.notch);

        //全屏显示
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

        WindowManager.LayoutParams lp = getWindow().getAttributes();
        
        //下面图1
        lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER;
        //下面图2
//        lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
        //下面图3
//        lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT;
        getWindow().setAttributes(lp);
    }
}

这里设置为全屏的显示效果,三种模式的结果如下图所示:

图一.png
LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER
图二.png
LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
图三.png
LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT
图三同样是黑边。
可以看到:
  1. LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES模式会让屏幕到延伸刘海区域中。
  2. LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER模式不会让屏幕到延伸刘海区域中,会留出一片黑色区域。
  3. LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT模式在全屏显示下跟LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER一样。
    注意:如果使用getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);实现全屏的话,即使使用LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES依然无法延伸到刘海区域中,需要使用如下设置解决:
decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_FULLSCREEN
        );

我们再来看看LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT模式在沉浸式状态栏下的效果,代码如下:

public class NotchActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //开局就一张背景图
        setContentView(R.layout.notch);

        //全屏显示
//        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

        //沉浸式状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        WindowManager.LayoutParams lp = getWindow().getAttributes();

        lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT;

        getWindow().setAttributes(lp);

    }
}

如下图所示:

image

可以看到:

当刘海区域完全在系统的状态栏时,LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT的显示效果与LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES一致。

所以,当我们进行刘海屏的适配时,请根据实际情况去使用不同的layoutInDisplayCutoutMode

2.6 那么刘海屏该如何适配呢?

2.6.1 如果页面存在状态栏

2.6.2 如果页面是全屏显示

3.Android P之前的刘海屏适配

上面是Android P才有的解决方案,在P之前呢,上面的代码通通都没用。然而我们伟大的国产厂商在Android P之前(基本都是Android O)就用上了高档大气上档次的刘海屏,所以,这也造就了各大厂商在Android P之前的解决方案百花齐放。下面,我们来看下主流厂商:华为、vivo、OPPO、小米等所提供的方案。

注:相关的代码都已封装好,可以直接拷贝使用。

3.1 华为

3.1.1 使用刘海区显示

使用新增的meta-data属性android.notch_support
在应用的AndroidManifest.xml中增加meta-data属性,此属性不仅可以针对Application生效,也可以对Activity配置生效。
如下所示:

<meta-data android:name="android.notch_support" android:value="true"/>

实际上还有一种代码实现的方式,不过代码比较多,这里就不贴了,有兴趣的话可以在文末的链接中点进去看看。

3.1.2 是否有刘海屏

通过以下代码即可知道华为手机上是否有刘海屏了,true为有刘海,false则没有。

    public static boolean hasNotchAtHuawei(Context context) {
        boolean ret = false;
        try {
            ClassLoader classLoader = context.getClassLoader();
            Class HwNotchSizeUtil = classLoader.loadClass("com.huawei.android.util.HwNotchSizeUtil");
            Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
            ret = (boolean) get.invoke(HwNotchSizeUtil);
        } catch (ClassNotFoundException e) {
            Log.e("Notch", "hasNotchAtHuawei ClassNotFoundException");
        } catch (NoSuchMethodException e) {
            Log.e("Notch", "hasNotchAtHuawei NoSuchMethodException");
        } catch (Exception e) {
            Log.e("Notch", "hasNotchAtHuawei Exception");
        } finally {
            return ret;
        }
    }

3.1.3 刘海尺寸

华为提供了接口获取刘海的尺寸,如下:

    //获取刘海尺寸:width、height
    //int[0]值为刘海宽度 int[1]值为刘海高度
    public static int[] getNotchSizeAtHuawei(Context context) {
        int[] ret = new int[]{0, 0};
        try {
            ClassLoader cl = context.getClassLoader();
            Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
            Method get = HwNotchSizeUtil.getMethod("getNotchSize");
            ret = (int[]) get.invoke(HwNotchSizeUtil);
        } catch (ClassNotFoundException e) {
            Log.e("Notch", "getNotchSizeAtHuawei ClassNotFoundException");
        } catch (NoSuchMethodException e) {
            Log.e("Notch", "getNotchSizeAtHuawei NoSuchMethodException");
        } catch (Exception e) {
            Log.e("Notch", "getNotchSizeAtHuawei Exception");
        } finally {
            return ret;
        }
    }

3.2 vivo

vivo在设置--显示与亮度--第三方应用显示比例中可以切换是否全屏显示还是安全区域显示。

3.2.1 是否有刘海屏

    public static final int VIVO_NOTCH = 0x00000020;//是否有刘海
    public static final int VIVO_FILLET = 0x00000008;//是否有圆角

    public static boolean hasNotchAtVivo(Context context) {
        boolean ret = false;
        try {
            ClassLoader classLoader = context.getClassLoader();
            Class FtFeature = classLoader.loadClass("android.util.FtFeature");
            Method method = FtFeature.getMethod("isFeatureSupport", int.class);
            ret = (boolean) method.invoke(FtFeature, VIVO_NOTCH);
        } catch (ClassNotFoundException e) {
            Log.e("Notch", "hasNotchAtVivo ClassNotFoundException");
        } catch (NoSuchMethodException e) {
            Log.e("Notch", "hasNotchAtVivo NoSuchMethodException");
        } catch (Exception e) {
            Log.e("Notch", "hasNotchAtVivo Exception");
        } finally {
            return ret;
        }
    }

3.2.2 刘海尺寸

vivo不提供接口获取刘海尺寸,目前vivo的刘海宽为100dp,高为27dp。

image

3.3 OPPO

OPPO目前在设置 -- 显示 -- 应用全屏显示 -- 凹形区域显示控制,里面有关闭凹形区域开关。

3.3.1 是否有刘海屏

    public static boolean hasNotchAtOPPO(Context context) {
        return context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
    }

3.3.2 刘海尺寸

OPPO不提供接口获取刘海尺寸,目前其有刘海屏的机型尺寸规格都是统一的。不排除以后机型会有变化。
其显示屏宽度为1080px,高度为2280px。刘海区域则都是宽度为324px, 高度为80px。

image

3.4 小米

3.4.1 是否有刘海屏

系统增加了 property ro.miui.notch,值为1时则是 Notch 屏手机。

手头上没有小米8的手机,暂时没法验证,这里就不贴代码了,免得误导大家。后面测试过再放出来。

3.4.2 刘海尺寸

小米的状态栏高度会略高于刘海屏的高度,因此可以通过获取状态栏的高度来间接避开刘海屏,获取状态栏的高度代码如下:

    public static int getStatusBarHeight(Context context) {
        int statusBarHeight = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
        }
        return statusBarHeight;
    }

其他手机也可以通过这个方法来间接避开刘海屏,但是有可能有些手机的刘海屏高度会高于状态栏的高度,所以这个方法获取到的结果并不一定安全。

上一篇 下一篇

猜你喜欢

热点阅读