Android bangs screen, full screen drop screen adaptation scheme

I will be fit after finishing the program, packaged into a library and upload to github, can use and reference

Project Address: github.com/smarxpan/No...

Screen size and market a wide variety of full-screen programs.

Here I use a chart to illustrate millet:

Both screens can be collectively referred to as fringe screen, but for smaller right-side bangs, the industry generally referred to as water droplets screen or widow's peak. For ease of explanation, hereinafter referred to "bangs screen" "fringe area" all refer to the same time on the views of two screens.

When we talk about screen adaptation, what are we talking about

  1. Longer-screen adaptation
  2. Prevent the contents bangs

The first point is that all applications require adaptation, corresponding to the following声明最大长宽比

And the second point, if the application itself does not need a full-screen display or use the immersion status bar is adapted not required.

For applications requiring a second fitting point, we need to acquire the position and the width and height of the fringe, and the display content can be avoided.

Disclaimer maximum aspect ratio

Former general screen aspect ratio of 16: 9, full-screen phone screen aspect ratio increased a lot, if you do not fit, then it will look something like this:

Black areas of unused areas.

Adaptation mode

There are two ways adaptation:

  1. The targetSdkVersion version is set to API 24 and over

    This operation will for the <application>tag implicitly adding a property android:resizeableActivity="true", the property will later action described in detail.

  2. In <application>increasing the Properties tab:android:resizeableActivity="false"

    While adding a node in the meta-datalabel:

     <!-- 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" />
    复制代码

Rationale

Here it comes to the knowledge that android: resizeableActivity property.

In Android 7.0 (API level 24) or a later application, android: resizeableActivity property defaults to true (corresponding to the adaptation mode 1). This property is controlled multi-window display, determines the current application or Activity supports multiple windows.

Multi-window support

In the list <activity>or <application>set the property node, enable or disable multi-window display:

android:resizeableActivity=["true" | "false"]
复制代码

If this property is set to true, Activity will be activated to split-screen and a free-form mode. If this property is set to false, Activity will not support multi-window mode. If the value is false, and the user attempts to start Activity in the multi-window mode, the full screen will Activity.

Mode 2 is the adaptation set the maximum aspect ratio of the screen, this is the way to set the official offer.

If you set the maximum aspect ratio must android:resizeableActivity="false". Otherwise, the maximum aspect ratio of no effect.

Liu screen adaptation

Android9.0 and above adaptation

Android P (9.0) begin, the official shaped screen adaptation of the way.

Support display cutouts

DisplayCutout by the new class, can determine the location and shape of the non-functional regions, these regions should not be displayed content. To determine whether there is a recess area of ​​the screen and its location, use getDisplayCutout () function.

  1. The new window layout properties layoutInDisplayCutoutMode make your application can be laid for the content around the device notch screen. You can use this property to one of the following values:

    • LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT
    • LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
    • LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER

    The default value is LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT, fringe area without a display, you need to set the valueLAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES

  2. You can simulate the screen as follows gaps on any device or emulator running Android P's:

    1. Enable Developer Options.
    2. In the Developer options screen, scroll down to the Drawing section and select Simulate a display with a cutout.
    3. Select the notch size of the screen.
  3. Adapter Reference:

     // 延伸显示区域到刘海
     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);
    复制代码

    Wherein the display region extends to the fringe code, can be achieved by modifying the applications Activity or style, for example:

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

Android O adapter

Due to Google's official Android P adaptation scheme was launched, so the Android O devices, various manufacturers have their own implementations.

I am here mainly adaptation Huawei, millet, oppo, three gave a complete solution. As vivo, vivo to determine whether the bangs screen API, but useless fringe area is set to display API, so no adapter.

Huawei's Android O adapter device

Option One:

  1. DETAILED DESCRIPTION follows:

     <meta-data android:name="android.notch_support" android:value="true"/>
    复制代码
  2. Right on Application special treatment to take effect, it means that all the pages, the application system will not make a special portrait scene down or horizontal screen scene:

     <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 for entry into force, which means you can be bangs screen adaptation for a single page, set the Activity of the property system will not do special treatment:

     <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>
    复制代码

Option II

Right on Application special treatment to take effect, it means that all the pages, the application system will not make a special portrait scene down or landscape scene

My NotchScreenTool in use is the second scheme, if required for Activity, amend its own recommendations.

  1. Settings window screen mobile phone use in Huawei fringe bangs area

     /*刘海屏全屏显示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. Clear added Huawei bangs screen Flag, recovery application does not use bangs display area

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

Millet Android O adapter device

  1. Determine whether the bangs screen

     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. Set the display area to the fringe

     @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. Get bangs width and height

     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 adapter device

  1. Determine whether the bangs screen

     @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. Obtaining coordinate left and right corners of the bangs

     /**
      * 获取刘海的坐标
      * <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 models do not need to set the display to the fringe area, just set up a full-screen application will be the default display.

Therefore Oppo models must adapt.

Adaptation summary

According to the above features, I will sort it became a dependent libraries: NotchScreenTool

It is simple to use:

// 支持显示到刘海区域
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());
            }
        }
    }
});
复制代码

Fringe area after obtaining information as needed for your application, to avoid the important controls.

For details, please refer to the code I project.

Reference links

Declare limited screen support: declare the maximum aspect ratio

Android 8.1 Compatibility Definition

Multi-window support

Support display cutouts

Huawei bangs screen phone version of Android O adapter guide

OPPO concave screen adaptation Description

Full screen adaptation vivo application guide

Millet bangs screen drop screen Android O adapter

Reproduced in: https: //juejin.im/post/5cf635846fb9a07f0c466ea7

Guess you like

Origin blog.csdn.net/weixin_34368949/article/details/91429378