推广

Android刘海屏、水滴屏全面屏适配方案

iseeyu2年前 (2024-02-22)推广131

上述两种屏幕都可以统称为刘海屏,不过对于右侧较小的刘海,业界一般称为水滴屏或美人尖。为便于说明,后文提到的「刘海屏」「刘海区」都同时指代上图两种屏幕。

当我们在谈屏幕适配时,我们在谈什么

  1. 适应更长的屏幕
  2. 防止内容被刘海遮挡

其中第一点是所有应用都需要适配的,对应下文的声明最大长宽比

而第二点,如果应用本身不需要全屏显示或使用沉浸式状态栏,是不需要适配的。

针对需要适配第二点的应用,需要获取刘海的位置和宽高,然后将显示内容避开即可。

声明最大长宽比

以前的普通屏长宽比为16:9,全面屏手机的屏幕长宽比增大了很多,如果不适配的话就会类似下面这样:

黑色区域为未利用的区域。

适配方式

适配方式有两种:

  1. 将targetSdkVersion版本设置到API 24及以上

    这个操作将会为<application> 标签隐式添加一个属性,android:resizeableActivity="true", 该属性的作用后面将详细说明。

  2. <application> 标签中增加属性:android:resizeableActivity="false"

    同时在节点下增加一个meta-data标签:

     <!-- Render on full screen up to screen aspect ratio of 2.4 -->
     <!-- Use a letterbox on screens larger than 2.4 -->
     <meta-data android:name="android.max_aspect" android:value="2.4" />
    
    

原理说明

这里涉及到的知识点是android:resizeableActivity属性。

在 Android 7.0(API 级别 24)或更高版本的应用,android:resizeableActivity属性默认为true(对应适配方式1)。这个属性是控制多窗口显示的,决定当前的应用或者Activity是否支持多窗口。

多窗口支持

在清单的<activity><application>节点中设置该属性,启用或禁用多窗口显示:

android:resizeableActivity=["true" | "false"]

如果该属性设置为 true,Activity 将能以分屏和自由形状模式启动。 如果此属性设置为 false,Activity 将不支持多窗口模式。 如果该值为 false,且用户尝试在多窗口模式下启动 Activity,该 Activity 将全屏显示。

适配方式2即为设置屏幕的最大长宽比,这是官方提供的设置方式。

如果设置了最大长宽比,必须android:resizeableActivity="false"。 否则最大长宽比没有任何作用。

适配刘海屏

Android9.0及以上适配

Android P(9.0)开始,官方提供了适配异形屏的方式。

Support display cutouts

通过全新的 DisplayCutout 类,可以确定非功能区域的位置和形状,这些区域不应显示内容。 要确定这些凹口屏幕区域是否存在及其位置,请使用 getDisplayCutout() 函数。

  1. 全新的窗口布局属性 layoutInDisplayCutoutMode 让您的应用可以为设备凹口屏幕周围的内容进行布局。 您可以将此属性设为下列值之一:

    • LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT
    • LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
    • LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER

    默认值是LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT,刘海区域不会显示内容,需要将值设置为LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES

  2. 您可以按如下方法在任何运行 Android P 的设备或模拟器上模拟屏幕缺口:

    1. 启用开发者选项。
    2. 在 Developer options 屏幕中,向下滚动至 Drawing 部分并选择 Simulate a display with a cutout。
    3. 选择凹口屏幕的大小。
  3. 适配参考:

     // 延伸显示区域到刘海
     WindowManager.LayoutParams lp = window.getAttributes();
     lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
     window.setAttributes(lp);
     // 设置页面全屏显示
     final View decorView = window.getDecorView();
     decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    
    

    其中延伸显示区域到刘海的代码,也可以通过修改Activity或应用的style实现,例如:

     <?xml version="1.0" encoding="utf-8"?>
     <resources>
         <style name="AppTheme" parent="xxx">
             <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
         </style>
     </resources>
    
    

Android O 适配

因Google官方的适配方案到Android P才推出,因此在Android O设备上,各家厂商有自己的实现方案。

我这里主要适配了华为、小米、oppo,这三家都给了完整的解决方案。至于vivo,vivo给了判断是否刘海屏的API,但是没用设置刘海区域显示到API,因此无需适配。

适配华为Android O设备

方案一:

  1. 具体方式如下所示:

     <meta-data android:name="android.notch_support" android:value="true"/>
    
    
  2. 对Application生效,意味着该应用的所有页面,系统都不会做竖屏场景的特殊下移或者是横屏场景的右移特殊处理:

     <application
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
         android:label="@string/app_name"
         android:roundIcon="@mipmap/ic_launcher_round"
         android:testOnly="false"
         android:supportsRtl="true"
         android:theme="@style/AppTheme">
         <meta-data android:name="android.notch_support" android:value="true"/>
         <activity android:name=".MainActivity">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN"/>
                 <category android:name="android.intent.category.LAUNCHER"/>
             </intent-filter>
     </activity>
    
    
  3. 对Activity生效,意味着可以针对单个页面进行刘海屏适配,设置了该属性的Activity系统将不会做特殊处理:

     <application
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
         android:label="@string/app_name"
         android:roundIcon="@mipmap/ic_launcher_round"
         android:testOnly="false"
         android:supportsRtl="true"
         android:theme="@style/AppTheme">
         <activity android:name=".MainActivity">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN"/>
    
                 <category android:name="android.intent.category.LAUNCHER"/>
             </intent-filter>
         </activity>
         <activity android:name=".LandscapeFullScreenActivity" android:screenOrientation="sensor">
         </activity>
         <activity android:name=".FullScreenActivity">
             <meta-data android:name="android.notch_support" android:value="true"/>
         </activity>
     </application>
    
    

方案二

对Application生效,意味着该应用的所有页面,系统都不会做竖屏场景的特殊下移或者是横屏场景的右移特殊处理

我的NotchScreenTool中使用的就是方案二,如果需要针对Activity,建议自行修改。

  1. 设置应用窗口在华为刘海屏手机使用刘海区

     /*刘海屏全屏显示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");
         }
     }
    
    
  2. 清除添加的华为刘海屏Flag,恢复应用不使用刘海区显示

     /**
      * 设置应用窗口在华为刘海屏手机使用刘海区
      * @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");
         }
     }
    
    

适配小米Android O设备

  1. 判断是否是刘海屏

     private static boolean isNotch() {
         try {
             Method getInt = Class.forName("android.os.SystemProperties").getMethod("getInt", String.class, int.class);
             int notch = (int) getInt.invoke(null, "ro.miui.notch", 0);
             return notch == 1;
         } catch (Throwable ignore) {
         }
         return false;
     }
    
    
  2. 设置显示到刘海区域

     @Override
     public void setDisplayInNotch(Activity activity) {
         int flag = 0x00000100 | 0x00000200 | 0x00000400;
         try {
             Method method = Window.class.getMethod("addExtraFlags",
                     int.class);
             method.invoke(activity.getWindow(), flag);
         } catch (Exception ignore) {
         }
     }
    
    
  3. 获取刘海宽高

     public static int getNotchHeight(Context context) {
         int resourceId = context.getResources().getIdentifier("notch_height", "dimen", "android");
         if (resourceId > 0) {
             return context.getResources().getDimensionPixelSize(resourceId);
         }
         return 0;
     }
    
     public static int getNotchWidth(Context context) {
         int resourceId = context.getResources().getIdentifier("notch_width", "dimen", "android");
         if (resourceId > 0) {
             return context.getResources().getDimensionPixelSize(resourceId);
         }
         return 0;
     }
    
    

适配oppoAndroid O设备

  1. 判断是否是刘海屏

     @Override
     public boolean hasNotch(Activity activity) {
         boolean ret = false;
         try {
             ret = activity.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
         } catch (Throwable ignore) {
         }
         return ret;
     }
    
    
  2. 获取刘海的左上角和右下角的坐标

     /**
      * 获取刘海的坐标
      * <p>
      * 属性形如:[ro.oppo.screen.heteromorphism]: [378,0:702,80]
      * <p>
      * 获取到的值为378,0:702,80
      * <p>
      * <p>
      * (378,0)是刘海区域左上角的坐标
      * <p>
      * (702,80)是刘海区域右下角的坐标
      */
     private static String getScreenValue() {
         String value = "";
         Class<?> cls;
         try {
             cls = Class.forName("android.os.SystemProperties");
             Method get = cls.getMethod("get", String.class);
             Object object = cls.newInstance();
             value = (String) get.invoke(object, "ro.oppo.screen.heteromorphism");
         } catch (Throwable ignore) {
         }
         return value;
     }
    
    

Oppo Android O机型不需要设置显示到刘海区域,只要设置了应用全屏就会默认显示。

因此Oppo机型必须适配。

适配总结

根据上述功能,我将其整理成了一个依赖库:NotchScreenTool

使用起来很简单:

// 支持显示到刘海区域
NotchScreenManager.getInstance().setDisplayInNotch(this);
// 获取刘海屏信息
NotchScreenManager.getInstance().getNotchInfo(this, new INotchScreen.NotchScreenCallback() {
    @Override
    public void onResult(INotchScreen.NotchScreenInfo notchScreenInfo) {
        Log.i(TAG, "Is this screen notch? " + notchScreenInfo.hasNotch);
        if (notchScreenInfo.hasNotch) {
            for (Rect rect : notchScreenInfo.notchRects) {
                Log.i(TAG, "notch screen Rect =  " + rect.toShortString());
            }
        }
    }
});

获取刘海区域信息后就可以根据自己应用的需要,来避开重要的控件。

详情可参考我项目中的代码。

参考资料

声明受限屏幕支持:声明最大长宽比

Android 8.1 兼容性定义

多窗口支持

Support display cutouts

华为刘海屏手机安卓O版本适配指导

OPPO凹形屏适配说明

vivo 全面屏应用适配指南

小米刘海屏水滴屏 Android O 适配

扫描二维码推送至手机访问。

版权声明:本文由西安泽虎代运营发布,如需转载请注明出处。

转载请注明出处https://www.0291.com.cn/post/56543.html

相关文章

开拼多多店铺需要投资多少钱?揭秘你不知道的秘密!

开拼多多店铺需要投资多少钱?揭秘你不知道的秘密!

随着电商的发展,越来越多的人选择在拼多多这样的电商平台上开店创业。那么,开拼多多店铺需要投资多少钱呢?我将为你揭秘这个你不知道的秘密! 我们要明确,开拼多多店铺的投资主要分为几个部分:店铺租金、商品采购、物流运输、店铺装修和运营推广等。 其一,店铺租金。在拼多多上开店是免费的,但是...

教育行业该怎么做推广?合适的推广渠道有哪些?

教育行业该怎么做推广?合适的推广渠道有哪些?

哈喽,大家好,今天媒介盒子小编又来跟大家分享软文的了,本篇分享的主要是:教育行业该怎么做推广?合适的推广有哪些?  教育行业属于朝阳产业,它随着互联网的快速发展,需求只会越来越大,教育行业竞争只会越来越激烈。么在互联网大环境下,教育行业该怎么推广呢?一起往下看看吧!  一...

如何进行网络营销和网站优化,帮助企业网站取得更出色的网络营销效果。

如何进行网络营销和网站优化,帮助企业网站取得更出色的网络营销效果。

在为广大企业客户提供服务咨询的过程中,笔者深入分析研究了大量网络营销成功案例,总结出“SEO优化理论”,在保证网站执行严格标准的UTF-8编码结构的基础上,开发了网站的实用功能。运用“SEO网络营销理论”实现网站功能流程和客户口碑的转变。帮助企业网站取得更出色的网络营销效果。 SEO网络...

富人能给普通人带来什么?答案出乎你意料

富人能给普通人带来什么?答案出乎你意料

“假如有一天我变得很有钱……”就像歌里唱的那样,人人都有富豪梦,渴望财富自由,渴望挥金如土。我们还在幻想,但早有人已经过上了普通人梦寐以求的。他们就是——超级富豪。现实世界里,全球85个人掌握着全球50%的财富。报纸上、新闻里总能看到巨富的身影。普遍观点是,一个国家的富人越...

Python面向对象实战:学生教师信息管理(5)

Python面向对象实战:学生教师信息管理(5)

学号必须以95开头5位数 三、添加学生信息 1. 需求分析 我们希望打开添加学生信息窗口后,填入学生信息,点击保存按钮后,数据会立即显示在学校信息列表里。 2. 实现过程 为了获取填入的信息,我们在studentservices中定义add方法,将student_current中...

巧借百度站长的后台工具,单靠内容自动推送功能也可以快速做排名。

巧借百度站长的后台工具,单靠内容自动推送功能也可以快速做排名。

许多刚接触SEO的新手或企业经常会有这样的问题:“网站已经上线半个月了,为什么不包括主页呢?”相信很多SEO朋友都知道,在没有外链的前提下,网站要被搜索引擎迅速收录,是非常困难的。其实,只要你能很好地利用百度站长的后台工具,你还可以实现快速收录的效果。那么,如何操作它呢?现在让我们一起去了解它。...

现在,非常期待与您的又一次邂逅

我们努力让每一部企业宣传片和抖音短视频成为商业大片