Adaptation and processing of Android Liu Haiping

Liu Haiping:

A notch is an area on the display of some devices that extends into the display surface to provide users with a full-screen experience while leaving room for important sensors on the front of the device. Android officially supports cutouts on devices running Android 9 (API level 28) and higher. Note that device manufacturers may also choose to support Notch on devices running Android 8.1 or lower

What are the requirements on devices with Notch

To ensure consistency and app compatibility, devices launching with Android 9 must ensure the following notch behaviors:

  • A fringe can contain at most one bang.
  • A device cannot have more than two notches.
  • There must be no notches on the two longer edges of the device.
  • In portrait mode with no special flag set, the height of the status bar must be at least equal to the height of the notch.
  • By default, the entire notch area must display a black border in fullscreen or landscape mode.

How to treat the bangs area:

To render content into the notch area, you can use  WindowInsets.getDisplayCutout()  to retrieve  the DisplayCutout  object

View decorView = getWindow().getDecorView();
if (decorView != null) {
    WindowInsets insets=  decorView.getRootWindowInsets();

}

Make sure your content doesn't overlap the status bar and navigation bar if you don't want your content to overlap the notch area

The window layout property  layoutInDisplayCutoutMode  controls how your content is rendered in the cutout area:

LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT - This is the default behavior, as described above. In portrait mode, the content will appear in the notch area; but in landscape mode, the content will display black borders.
LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES - In both portrait and landscape mode, the content is rendered into the cutout area.
LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER - Content is never rendered into the notch area.

    <style name="ActivityTheme">
      <item name="android:windowLayoutInDisplayCutoutMode">
        shortEdges <!-- default, shortEdges, never -->
      </item>
    </style>

Present content in the short side notch area

For some content, such as videos, photos, maps, and games, it's a good idea to render in the notch to provide users with a more immersive full-screen experience

If LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES is set  , the content extends to the notch area on the short edge of the display in both portrait and landscape modes, regardless of whether the system bars are hidden or visible

[*Note that Android may not allow content views to overlap system bars.  To override this behavior and force the content to extend into the notch area, apply any of the following flags to the view visibility via  the View.setSystemUiVisibility(int) method]

  • SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
  • SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
  • SYSTEM_UI_FLAG_LAYOUT_STABLE

If LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER is set  , the window is not allowed to overlap the cutout area.

This mode should be used for windows with View.SYSTEM_UI_FLAG_FULLSCREEN  or  View.SYSTEM_UI_FLAG_HIDE_NAVIGATION temporarily set   , to avoid performing another window layout when the flag is set or cleared.

Best Practices for Supporting Liu Haiping

1. Don't let the notch area obscure any important text, controls, or other information.

2. Do not place or extend any interactive elements that require fine touch recognition into the notch area. The touch sensitivity in the notch area may be a little lower than other areas.

3. Avoid hardcoding the status bar height, as doing so may cause content to overlap or be cut off. If possible, use  WindowInsetsCompat  to retrieve the status bar height and determine the appropriate padding to apply to your content.

WindowInsets insets= getWindow().getDecorView().getRootWindowInsets();
if (insets==null)
    return;
int stateTop=insets.getStableInsetTop();
int windowTop=insets.getSystemWindowInsetTop();

4. Do not assume that the application will occupy the entire window, but use  View.getLocationInWindow()  to confirm the location of the application. Do not use  View.getLocationOnScreen() .

5. If your application needs to enter and exit full screen mode, please use  shortEdges or  never notch mode. The default notch behavior can cause content in the app to move up and down during fullscreen mode transitions

6. In full screen mode, be cautious when using window coordinates and screen coordinates, because your application will not occupy the entire screen when black borders are displayed. The coordinates obtained from the origin of the screen and the coordinates obtained from the origin of the window are no longer the same because of the black border. You can use getLocationOnScreen()  to convert screen coordinates to view coordinates as needed 

Guess you like

Origin blog.csdn.net/qq36246172/article/details/118759368#comments_26420943