The most comprehensive guide for Android folding screen adaptation is here

With the release of Samsung Galaxy Fold and Huawei Mate X, folding screen phones have begun to enter everyone's attention. While changing the mobile phone experience, it also brings more challenges to our developers in adaptation. This article introduces you to some concepts related to folding screens in Android development, and how to adapt them to folding screens.

Folding screen adaptation

The reason why the folding screen needs to be adapted is because the size of the screen on which our application is running may change. This situation will cause some problems for existing projects.

Therefore, the essence of folding screen adaptation is: when the application is running, the size, density, or ratio of the screen changes, and the application can continue to display and run normally on the changed screen.

In fact, this situation does not occur after the appearance of the folding screen. The same situation occurs in the vertical and horizontal switching of applications, but many applications are forced to be vertical and do not need to deal with this adaptation.

Allow changing app size

To adapt to the folding screen, the first thing is to let the application support dynamic change of size, we need to declare under the Application or the corresponding Activity in the menifest:

android:resizeableActivity="true"

 

On the contrary, if you don't plan to adapt for the time being, just set this parameter to false.

It should be noted that this parameter defaults to true in Android 7.0 or higher, and the following defaults to false.

Two concepts related to this parameter are introduced below.

Split screen mode

The reason why resizeableActivity is changed to true by default from Android 7.0 is because a new feature has been added in 7.0, called split screen mode.

If resizeableActivity is set to false, it means that the app does not support split-screen mode, and it determines whether the app has a split-screen setting.

Compatibility mode

When resizeableActivity is set to false, the unfolding and folding screen may become such an effect:

This effect is similar to using an incompatible iPhone app on the iPad. This mode filled with black around it is called compatibility mode.

The display of compatibility mode is related to the maximum supported ratio maxAspectRatio. When the screen ratio exceeds maxAspectRatio, it will be filled with black borders. The official recommendation is to set maxAspectRatio to 2.4 (12: 5). The method to modify maxAspectRatio is as follows:

  • Android 8.0 or above

Configure android:maxAspectRatio in the <activity> tag:

 

<activity android:name=".MainActivity"
          android:maxAspectRatio="2.4" />
  • Android 8.0 or lower

Add a meta-data named android.max_aspect in the <application> tag:

 

<meta-data android:name="android.max_aspect" android:value="2.4" />

If resizeableActivity is set to true, there is no need to set maxAspectRatio, and it will not take effect if it is set.

Monitor size change

By default, when the screen changes, the system will destroy and recreate the entire Activity. But we hope that after the screen changes, the program can continue to run in the state before the switch, without restarting the page.

We can add configuration to Activity:

 

android:configChanges="screenSize|smallestScreenSize|screenLayout"

After this configuration, when the screen changes, the Activity will not be restarted, and the onConfigurationChanged method will be called. We can get the current screen information in this method:

 

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    Log.i("config", "newConfig.screenHeightDp:" + newConfig.screenHeightDp
            + ", newConfig.screenWidthDp" + newConfig.screenWidthDp);
}

After this change, you need to pay attention to testing to see if the layout of the page is disordered. If the layout is unreasonable, you need to modify the layout to adapt to different resolutions.

We can also update the layout according to the screen information, such as switching LinearLayout to GridLayout on the big screen to make full use of the display space of the big screen. This is a further optimization approach:

Android Q

On the upcoming Android Q, some features that support folding screens have been added.

Multi-resume

For the split-screen mode, the previous split-screen only supported the simultaneous display of two applications, while the large screen has brought more possibilities. Now it has allowed more than two applications to be displayed at the same time.

In previous versions of Android Q, in applications running in split-screen mode, only the activity that got the focus would be in the onResume state, and other visible activities would be in the onPause state.

On Android Q, all top-level visible activities are in the onResume state to ensure that the visible activities in split-screen mode can run normally. But there is still only one Activity that can get the focus. We call this Activity TopResumedActivity.

A life cycle callback method onTopResumedActivityChanged() has been added to the Activity of Android Q. It will be called when the Activity gets or loses focus. It can be used to determine whether the current Activity has focus:

 

protected void onTopResumedActivityChanged(boolean topResumed) {
    if (topResumed) {
        // 获取到焦点
    } else {
        // 失去焦点
    }
}

This method is used when we use exclusive resources. What is an exclusive resource? Microphones and cameras are, such resources can only be used by one activity at a time.

For example, multiple activities in split-screen mode all use the camera, but only the activity that has the focus has access rights. In this case, onTopResumedActivityChanged() is used to determine whether the current activity has the focus. It is not necessary to release the camera when the focus is lost, but you need to deal with the situation that the camera is disconnected and reconnected.

minAspectRatio

Before Android Q, only the maximum support ratio maxAspectRatio can be configured. Now Android Q can configure the minimum support ratio minAspectRatio. The
usage is the same as maxAspectRatio:

 

<activity android:name=".MainActivity"
          android:maxAspectRatio="2.4"
          android:minAspectRatio="1"/>

The maximum and minimum support ratio is only useful when resizeableActivity is set to false.

debugging

Of course, the best debugging tool is to use the real machine, but currently only a small number of people have this condition. The following are two debugging schemes other than the real machine.

Android Studio

In Android Studio 3.5, a virtual machine for folding screen devices has been added. We can create one to debug:

By clicking the button on the simulator, we can switch the collapsed and expanded state of the virtual machine:

Command Line

We can dynamically modify the resolution of the mobile phone through the command line to achieve the effect of simulating folding screen switching. Taking the resolution of Mate X as an example, we first use the command line:

 

adb shell wm size 1148x2480

The phone resolution will be simulated as 1148x2480, which is the resolution of Mate X when it is folded, then enter:

 

adb shell wm size 2200x2480

The mobile phone resolution is modified to the expanded resolution of Mate X 2200x2480, which simulates the switching of the folding screen expansion.

You can modify the resolution to 1148x2480 again to simulate the switching of screen folding. Finally, after playing, use the following command line to restore the resolution of the phone itself:

 

adb shell wm size reset

to sum up

In general, if you want to adapt to the folding screen, the first step is to set resizeableActivity to true, then configure configChanges for the Activity and test it. Finally, you can go one step closer and design another set of UI for the big screen, and switch the UI when the folding screen is switched.

 

 

Guess you like

Origin blog.csdn.net/weixin_40611659/article/details/108747360