刘海屏适配总结

0.刘海屏

这儿的刘海屏统一指挖孔屏、凹凸屏等有一部分不能显示内容的区域的屏幕

1.刘海屏适配

1.1刘海屏的类型

自从iPhone X以后,Android各大厂商都在跟风刘海屏,先不管它的好看与否,这都是android开发者迈不过去的一道坎,由于android设备类型太多,也造成了刘海屏的不同类型,大致如下图所示
在这里插入图片描述
最后一种没有见过,暂时不去管它。为了防止全屏显示时布局的遮挡及提供给用户更好的屏幕使用或者更好的体验等(如:全屏视频,游戏)我们在之后的时间中必须要进行刘海屏的适配。

1.2 Google文档对刘海屏设备的要求

为了确保一致性和应用兼容性,搭载 Android 9 的设备必须确保以下刘海行为:

  • 一条边缘最多只能包含一个刘海
  • 一台设备不能有两个以上的刘海
  • 设备的两条较长边缘上不能有刘海
  • 在未设置特殊标志的竖屏模式下,状态栏的高度必须至少与刘海的高度持平
  • 默认情况下,在全屏模式或横屏模式下,整个刘海区域必须显示黑边

2. Android O的解决方案

由于Google在Android P以上的设备上才正式提供刘海屏的支持,所以在Android O的设备上我们只能去各大厂商的官方文档寻找刘海屏适配的支持。

2.1 华为刘海屏Android O适配
2.1.1 设置华为刘海屏的适配

方案一: 在AndroidManifest.xml文件中增加meta-data属性,如下

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

此方式可以在application标签下,对所有的页面生效,也可以放在activity标签下,对特定的页面生效,意味着系统对所有的竖屏页面都不会做下移处理,或者对所有的横屏页面都不会做右移处理。
方案二: 动态添加/清除华为刘海屏的FLAG_NOTCH_SUPPORT,代码如下

/*刘海屏全屏显示FLAG*/
public static final int FLAG_NOTCH_SUPPORT=0x00010000;
/**
 * 设置应用窗口在华为刘海屏手机使用刘海区
 * @param window 应用页面window对象
 */
public static void setFullScreenWindowLayoutInDisplayCutout(Window window) {
    if (window == null) {
        return;
    }
    WindowManager.LayoutParams layoutParams = window.getAttributes();
    try {
        Class layoutParamsExCls = Class.forName("com.huawei.android.view.LayoutParamsEx");
        Constructor con = layoutParamsExCls.getConstructor(LayoutParams.class);
        Object layoutParamsExObj = con.newInstance(layoutParams);
        Method method = layoutParamsExCls.getMethod("addHwFlags",  int.class);
        method.invoke(layoutParamsExObj,  FLAG_NOTCH_SUPPORT);
    } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |InstantiationException 
    | InvocationTargetException e) {
        Log.e("test", "hw add notch screen flag api error");
    } catch (Exception e) {
        Log.e("test", "other Exception");
    }
}

清除华为刘海屏的FLAG_NOTCH_SUPPORT

/*刘海屏全屏显示FLAG*/
public static final int FLAG_NOTCH_SUPPORT=0x00010000;
/**
 * 设置应用窗口在华为刘海屏手机不使用刘海区
 * @param window 应用页面window对象
 */
public static void setNotFullScreenWindowLayoutInDisplayCutout (Window window) {
    if (window == null) {
        return;
    }
    WindowManager.LayoutParams layoutParams = window.getAttributes();
    try {
        Class layoutParamsExCls = Class.forName("com.huawei.android.view.LayoutParamsEx");
        Constructor con = layoutParamsExCls.getConstructor(LayoutParams.class);
        Object layoutParamsExObj = con.newInstance(layoutParams);
        Method method = layoutParamsExCls.getMethod("clearHwFlags",  int.class);
        method.invoke(layoutParamsExObj,  FLAG_NOTCH_SUPPORT);
    } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |InstantiationException 
    | InvocationTargetException e) {
        Log.e("test", "hw clear notch screen flag api error");
    } catch (Exception e) {
        Log.e("test", "other Exception");
    }
}

动态添加删除华为刘海屏的代码

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(isAdd) {//add flag
            isAdd = false;
            NotchSizeUtil.setFullScreenWindowLayoutInDisplayCutout(getWindow());           
            getWindowManager().updateViewLayout(getWindow().getDecorView(),getWindow().getDecorView().getLayoutParams());
        } else{//clear flag
            isAdd = true;
            NotchSizeUtil.setNotFullScreenWindowLayoutInDisplayCutout(getWindow());            
            getWindowManager().updateViewLayout(getWindow().getDecorView(),getWindow().getDecorView().getLayoutParams());
        }
    }
});
2.1.2 判断华为手机是否有刘海屏
public static boolean hasNotchInScreen(Context context) {
    boolean ret = false;
    try {
        ClassLoader cl = context.getClassLoader();
        Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
        Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
        ret = (boolean) get.invoke(HwNotchSizeUtil);
    } catch (ClassNotFoundException e) {
        Log.e("test", "hasNotchInScreen ClassNotFoundException");
    } catch (NoSuchMethodException e) {
        Log.e("test", "hasNotchInScreen NoSuchMethodException");
    } catch (Exception e) {
        Log.e("test", "hasNotchInScreen Exception");
    } finally {
        return ret;
    }
}
2.1.3 获取华为刘海尺寸
public static int[] getNotchSize(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("test", "getNotchSize ClassNotFoundException");
    } catch (NoSuchMethodException e) {
        Log.e("test", "getNotchSize NoSuchMethodException");
    } catch (Exception e) {
        Log.e("test", "getNotchSize Exception");
    } finally {
        return ret;
    }
}

在做适配的时候在华为官方文档找不到适配刘海屏的方案了,更多详细的内容参考android兼容huawei手机刘海屏解决方案

2.2 小米Android O刘海屏适配

小米跟华为的方案一样都是可以通过AndroidManifest.xml以及代码控制的

2.2.1 设置小米刘海屏适配

方案一: 在application下添加如下代码,表明刘海屏的适配

<meta-data
 android:name="notch.config"
 android:value="portrait|landscape"/>

value的取值有如下四种

"none" 横竖屏都不绘制耳朵区

"portrait" 竖屏绘制到耳朵区

"landscape" 横屏绘制到耳朵区

"portrait|landscape" 横竖屏都绘制到耳朵区

方案二: window级别的控制,在 WindowManager.LayoutParams 增加 extraFlags 成员变量,用以声明该 window 是否使用耳朵区。其中,extraFlags 有以下变量:

0x00000100 开启配置
0x00000200 竖屏配置
0x00000400 横屏配置

组合后如下

0x00000100 | 0x00000200 竖屏绘制到耳朵区
0x00000100 | 0x00000400 横屏绘制到耳朵区
0x00000100 | 0x00000200 | 0x00000400 横竖屏都绘制到耳朵区

控制 extraFlags 时注意只控制这几位,不要影响其他位。可以用 Window 的 addExtraFlags 和 clearExtraFlags 来修改, 这两个方法是 MIUI 增加的方法,需要反射调用。

int flag = 0x00000100 | 0x00000200 | 0x00000400;
try {
    Method method = Window.class.getMethod("addExtraFlags",  int.class);
    method.invoke(getWindow(), flag);
} catch (Exception e) {
    Log.i(TAG, "addExtraFlags not found.");
}
2.2.2 判断小米是否有刘海屏

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

SystemProperties.getInt("ro.miui.notch", 0) == 1;

代码如下

/**
 * 判断是否有刘海屏
 *
 * @param context
 * @return true:有刘海屏;false:没有刘海屏
 */
public static boolean hasNotch(Context context) {
    boolean ret = false;
    try {
        ClassLoader cl = context.getClassLoader();
        Class SystemProperties = cl.loadClass("android.os.SystemProperties");
        Method get = SystemProperties.getMethod("getInt", String.class, int.class);
        ret = (Integer) get.invoke(SystemProperties, "ro.miui.notch", 0) == 1;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        return ret;
    }
}
2.2.3 获取小米刘海的宽高

有些版本没有获取刘海宽高的方法,MIUI 10 新增了获取刘海宽和高的方法,需升级至8.6.26开发版及以上版本。以下是获取当前设备刘海宽高的方法:

//获取设备刘海高度
int resourceId = context.getResources().getIdentifier("notch_height", "dimen", "android");
if (resourceId > 0) {
	 result = context.getResources().getDimensionPixelSize(resourceId);
}
】
//获取设备刘海宽度
int resourceId = context.getResources().getIdentifier("notch_width", "dimen", "android");
if (resourceId > 0) {
	 result = context.getResources().getDimensionPixelSize(resourceId);
}

更加详细的内容请参照小米刘海屏Android O适配文档

2.3 Vivo、Oppo刘海屏Android O适配

Vivo跟Oppo没有具体的适配方案,关于Vivo的官方适配指导请参照Vivo全面屏应用适配指南,Oppo的官方指导请参考Oppo凹型屏适配说明

2.3.1 设置应用适配全面屏

方式一: 配置支持最大高宽比

<meta-data android:name="android.max_aspect"  android:value="ratio_float" />

android:maxAspectRatio="ratio_float"   (API LEVEL 26)

说明:以上两种接口可以二选一,ratio_float = 屏幕高 / 屏幕宽 (如oppo新机型屏幕分辨率为2280 x 1080, ratio_float = 2280 / 1080 = 2.11,建议设置 ratio_float为2.2或者更大)

方式二: 支持分屏,注意验证分屏下界面兼容性

android:resizeableActivity=“true”

建议采用方式二适配支持全面屏。详细参考官方文档

2.3.2 Oppo判断是否有刘海屏
/**
 * 判断是否有刘海屏
 *
 * @param context
 * @return true:有刘海屏;false:没有刘海屏
 */
public static boolean hasNotchInOppo(Context context){
    return context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
}
2.3.3 Vivo判断是否有刘海屏
public static final int VIVO_NOTCH = 0x00000020; // 是否有刘海
public static final int VIVO_FILLET = 0x00000008; // 是否有圆角

/**
 * 判断是否有刘海屏
 *
 * @param context
 * @return true:有刘海屏;false:没有刘海屏
 */
public static boolean hasNotch(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;
    }
}

剩下的就是对UI的修改了,要注意多测试,多参考官方指南

3. Android P的适配

这儿我们只记录如何做,至于更详细的细节请参考官方文档

3.1设置刘海屏样式

我们可以通过设置窗口属性 layoutInDisplayCutoutMode的值来控制布局如何显示

  • LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT - 这是默认行为,如上所述。在竖屏模式下,内容会呈现到刘海区域中;但在横屏模式下,内容会显示黑边。
  • LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES - 在竖屏模式和横屏模式下,内容都会呈现到刘海区域中。
  • LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER - 内容从不呈现到刘海区域中。

在代码中这样设置

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            // 始终允许窗口延伸到屏幕短边上的刘海区域
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            getWindow().setAttributes(lp);
        }

也可以通过主题样式设置刘海屏显示,如下

<style name="ActivityTheme">
      <item name="android:windowLayoutInDisplayCutoutMode">
        shortEdges <!-- default, shortEdges, never -->
      </item>
    </style>
3.2 UI的适配

我们可以通过获取DisplayCutout来获取刘海屏的区域,从而进行布局的修改。这段代码同样只能在Android P以上才能使用

	 final View decorView = getWindow().getDecorView();
     decorView.setOnApplyWindowInsetsListener((v, insets) -> {
                DisplayCutout displayCutout = insets.getDisplayCutout();
                if (displayCutout != null) {
                    int left = displayCutout.getSafeInsetLeft();
                    int top = displayCutout.getSafeInsetTop();
                    mDisplayCutEnd = displayCutout.getSafeInsetRight();
                    int bottom = displayCutout.getSafeInsetBottom();
                    //处理UI逻辑
                }
                return insets.consumeSystemWindowInsets();
            });

参考

Google刘海屏解决方案
android兼容huawei手机刘海屏解决方案
小米Android O刘海屏解决方案
Oppo Android O刘海屏解决方案
Vivo Android O刘海屏解决方案

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值