Android bangs screen, full screen adaptation drop screen

Now, the screen size of the market and a wide variety of full-screen programs. Here I use a chart to illustrate millet: the two 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.

Liu screen, full screen adaptation drop screen details

When we talk about screen adaptation, specifically what we talk about it?

  • Longer-screen adaptation

  • Prevent the contents bangs

The first point is that all applications require adaptation, the maximum aspect ratio corresponding to the following statements, and a 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:

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 add an attribute, android: resizeableActivity = "true" , will later This property is described in detail.

  1. <Application> tags added attributes: android: resizeableActivity = "false", while increasing at a meta- node>

 <!-- 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

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.

You may list <activity>or  <application>set the property node, enable or disable the multi-window display, configured as follows:

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 adaptation

Android P (9.0) start, the official start of digging holes provided official screen adaptation API, specifically refer to Support display cutouts. DisplayCutout class provided by the Android P, can determine the position 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.

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 can be set to the value needed LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES display.

You can simulation screen as follows notch on any device or emulator running Android P of:

  1. Enable Developer Options;

  2. In the Developer options screen, scroll down to the Drawing section and select Simulate a display with a cutout.

Reference Example adapted:

// 延伸显示区域到刘海 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 (8.0 version) equipment, various manufacturers have their own implementations.

Huawei Android O adapter

Option One:

  1. DETAILED DESCRIPTION follows:

<meta-data android:name="android.notch_support" android:value="true"/>
  1. Application for entry into force, which means all the pages, the system will not do the right application of special treatment down special portrait or landscape scene of the scene. E.g:

<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>
  1. 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. E.g:

<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 Two: to Application into effect, means that all the pages, the system will not do the right application of special treatment down special portrait or landscape scene of the scene.

1, set the application window using fringe bangs area in Huawei screen phone.

/*刘海屏全屏显示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.

 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

  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; }
  1. 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) {     } }
  1. 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; }

oppo Android O adapter

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

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; }

Product is slightly Library http://www.pinlue.com/

 

 

Guess you like

Origin blog.csdn.net/yihuliunian/article/details/91353604