current position:Home>Android bangs screen and water drop screen are the best overall adaptation scheme

Android bangs screen and water drop screen are the best overall adaptation scheme

2022-05-15 05:13:09InfoQ

  • Yes Application take effect , Means all pages of the app , The system will not do any special down shift of vertical screen or right shift of horizontal screen :

<applicationandroid:allowBackup=&quot;true&quot;android:icon=&quot;@mipmap/ic_launcher&quot;android:label=&quot;@string/app_name&quot;android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;android:testOnly=&quot;false&quot;android:supportsRtl=&quot;true&quot;android:theme=&quot;@style/AppTheme&quot;><meta-data android:name=&quot;android.notch_support&quot; android:value=&quot;true&quot;/><activity android:name=&quot;.MainActivity&quot;><intent-filter><action android:name=&quot;android.intent.action.MAIN&quot;/><category android:name=&quot;android.intent.category.LAUNCHER&quot;/></intent-filter></activity>

  • Yes Activity take effect , It means that the screen can be adapted to a single page , Set the Activity The system will not do special processing :

<applicationandroid:allowBackup=&quot;true&quot;android:icon=&quot;@mipmap/ic_launcher&quot;android:label=&quot;@string/app_name&quot;android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;android:testOnly=&quot;false&quot;android:supportsRtl=&quot;true&quot;android:theme=&quot;@style/AppTheme&quot;><activity android:name=&quot;.MainActivity&quot;><intent-filter><action android:name=&quot;android.intent.action.MAIN&quot;/>

<category android:name=&quot;android.intent.category.LAUNCHER&quot;/></intent-filter></activity><activity android:name=&quot;.LandscapeFullScreenActivity&quot; android:screenOrientation=&quot;sensor&quot;></activity><activity android:name=&quot;.FullScreenActivity&quot;><meta-data android:name=&quot;android.notch_support&quot; android:value=&quot;true&quot;/></activity></application>

Option two

Yes Application take effect , Means all pages of the app , The system will not do any special down shift of vertical screen or right shift of horizontal screen

my [NotchScreenTool](() Scheme 2 is used in , If you need to Activity, It is suggested to modify .

  • Set the application window in Huawei's Liu Haiping mobile phone use Liu Haiqu

/
A full screen display FLAG
/public static final int FLAG_NOTCH_SUPPORT=0x00010000;/**

  • Set the application window in Huawei's Liu Haiping mobile phone use Liu Haiqu
  • @param window  Application page window object */public static void setFullScreenWindowLayoutInDisplayCutout(Window window) {if (window == null) {return;}WindowManager.LayoutParams layoutParams = window.getAttributes();try {Class layoutParamsExCls = Class.forName(&quot;com.huawei.android.view.LayoutParamsEx&quot;);Constructor con=layoutParamsExCls.getConstructor(LayoutParams.class);Object layoutParamsExObj=con.newInstance(layoutParams);Method method=layoutParamsExCls.getMethod(&quot;addHwFlags&quot;, int.class);method.invoke(layoutParamsExObj, FLAG_NOTCH_SUPPORT);} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |InstantiationException| InvocationTargetException e) {Log.e(&quot;test&quot;, &quot;hw add notch screen flag api error&quot;);} catch (Exception e) {Log.e(&quot;test&quot;, &quot;other Exception&quot;);}}

  • Remove the added Huawei Liu Haiping Flag, Recovery application does not use banged area display

/**

  • Set the application window in Huawei's Liu Haiping mobile phone use Liu Haiqu
  • @param window  Application page window object */public static void setNotFullScreenWindowLayoutInDisplayCutout (Window window) {if (window == null) {return;}WindowManager.LayoutParams layoutParams = window.getAttributes();try {Class layoutParamsExCls = Class.forName(&quot;com.huawei.android.view.LayoutParamsEx&quot;);Constructor con=layoutParamsExCls.getConstructor(LayoutParams.class);Object layoutParamsExObj=con.newInstance(layoutParams);Method method=layoutParamsExCls.getMethod(&quot;clearHwFlags&quot;, int.class);method.invoke(layoutParamsExObj, FLAG_NOTCH_SUPPORT);} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |InstantiationException| InvocationTargetException e) {Log.e(&quot;test&quot;, &quot;hw clear notch screen flag api error&quot;);} catch (Exception e) {Log.e(&quot;test&quot;, &quot;other Exception&quot;);}}
Fit millet Android O equipment
  • Judge whether it's Liu Haiping

private static boolean isNotch() {try {Method getInt = Class.forName(&quot;android.os.SystemProperties&quot;).getMethod(&quot;getInt&quot;, String.class, int.class);int notch = (int) getInt.invoke(null, &quot;ro.miui.notch&quot;, 0);return notch == 1;} catch (Throwable ignore) {}return false;}

  • Set the display to the fringe area

@Overridepublic void setDisplayInNotch(Activity activity) {int flag = 0x00000100 | 0x00000200 | 0x00000400;try {Method method = Window.class.getMethod(&quot;addExtraFlags&quot;,int.class);method.invoke(activity.getWindow(), flag);} catch (Exception ignore) {}}

  • Get Liu haikuangao

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

public static int getNotchWidth(Context context) {int resourceId = context.getResources().getIdentifier(&quot;notch_width&quot;, &quot;dimen&quot;, &quot;android&quot;);if (resourceId > 0) {return context.getResources().getDimensionPixelSize(resourceId);}return 0;}
adapter oppoAndroid O equipment
  • Judge whether it's Liu Haiping

@Overridepublic boolean hasNotch(Activity activity) {boolean ret = false;try {r 
《Android Summary of learning notes + Latest mobile architecture video + Big Android interview questions + Project actual combat source code handout 》 Free open source Hui Xin search official account 【 Advanced programming 】
 et = activity.getPackageManager().hasSystemFeature(&quot;com.oppo.feature.screen.heteromorphism&quot;);} catch (Throwable ignore) {}return ret;}

  • Get the coordinates of the upper left corner and the lower right corner of the bangs

/**

  • Get the coordinates of the bangs
  • <p>
  • Attribute shape :[ro.oppo.screen.heteromorphism]: [378,0:702,80]
  • <p>
  • The value obtained is 378,0:702,80
  • <p>
  • <p>
  • (378,0) Is the coordinate of the upper left corner of the fringe area
  • <p>
  • (702,80) Is the coordinates of the lower right corner of the fringe area */private static String getScreenValue() {String value = &quot;&quot;;Class<?> cls;try {cls = Class.forName(&quot;android.os.SystemProperties&quot;);Method get = cls.getMethod(&quot;get&quot;, String.class);Object object = cls.newInstance();value = (String) get.invoke(object, &quot;ro.oppo.screen.heteromorphism&quot;);} catch (Throwable ignore) {}return value;}

Oppo Android O The model does not need to be displayed in the fringe area , As long as the application is set, the full screen will be displayed by default .

therefore Oppo The model must be adapted to .

Adaptation summary

According to the above functions , I organized it into a dependency Library :[NotchScreenTool](()

It's simple to use :

//  Support display to fringe area NotchScreenManager.getInstance().setDisplayInNotch(this);//  Get Liu Haiping information NotchScreenManager.getInstance().getNotchInfo(this, new INotchScreen.NotchScreenCallback() {@Overridepublic void onResult(INotchScreen.NotchScreenInfo notchScreenInfo) {Log.i(TAG, &quot;Is this screen notch? &quot; + notchScreenInfo.hasNotch);if (notchScreenInfo.hasNotch) {for (Rect rect : notchScreenInfo.notchRects) {

copyright notice
author[InfoQ],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/131/202205111301205479.html

Random recommended