The previous two articles have combed the concept of screen adaptation and explained the DP adaptation scheme and the width and height qualifier adaptation scheme. If you have not seen the children’s shoes, you can refer to these two articles first:

Android comprehensive screen adaptation solution analysis (a)

Android comprehensive screen adaptation solution analysis (2)

Common adaptation schemes are listed as follows:

  • Dp adaptation scheme
  • Width and height qualifier adaptation scheme
  • AndroidAutoLayout adaptation scheme
  • Sw qualifier adaptation scheme
  • Toutiao adaptation scheme
  • AndroidAutoSize adaptation scheme

Here it is necessary to reiterate that some outdated adaptation schemes are also explained here. Each adaptation scheme has its own advantages and disadvantages. From the original adaptation scheme, we can better understand why various adaptation schemes are derived.

3, AndroidAutoLayout adaptation scheme

The so-called AndroidAutoLayout adaptation scheme, in fact, is UI adaptation framework, this is hongyang big man wrote adaptation scheme, which is inspired by the width and height of the qualified adaptation scheme to write, the use is relatively simple, directly fill in the design of the pixel size can be completed adaptation, but the current framework has stopped maintenance. The following is an example to illustrate the integration process:

1. Introduce the adaptation framework

implementation 'com. Zhy: autolayout: 1.4.5'
Copy the code

2. Indicate the size of the design draft in the AndroidManifest configuration file of the project. Here the test takes 720×1280 as an example:

<! --> <meta-data Android :name="design_width"
            android:value="720" />

        <meta-data
            android:name="design_height"
            android:value="1280" />
Copy the code

3. Inherit the Activity you want to adapt from AutoLayoutActivity. If you don’t want to inherit from AutoLayoutActivity, you can write the layout file with:

  • LinearLayout -> AutoLinearLayout
  • RelativeLayout -> AutoRelativeLayout
  • FrameLayout -> AutoFrameLayout

This can also be used to complete the adaptation. Here I use an AutoRelativeLayout test with the following layout:

<? xml version="1.0" encoding="utf-8"? > <com.zhy.autolayout.AutoRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="250px"
        android:layout_height="250px"
        android:layout_centerInParent="true"
        android:src="@mipmap/ic_launcher" />

</com.zhy.autolayout.AutoRelativeLayout>
Copy the code

Test layout file is very simple, only set the picture, careful children also found that the width and height of the picture is set in px unit, instead of the usual dp unit, as shown in the picture:

Yes, you are not wrong, here can be used directly in the layout file specific pixel values, such as the design draft on the picture is 250 * 250, then we can directly write 250 px, runtime framework will help us according to the specific size scale expansion of different mobile phone, let’s take a look at the examples in different cell phone test contrast effect:

Let’s take a look at the test and comparison effect of different phones before using the AndroidAutoLayout adaptation scheme:

According to the comparison effect before and after adaptation is quite obvious, I mainly introduce the basic adaptation scheme here, if you want to know more detailed adaptation, you can go here and click here

There is no doubt that this UI framework is an excellent adaptation scheme, simple to use, all you need to do is copy the px above the design draft, directly write into the layout file to complete adaptation, and on the basis of the width and height qualifier adaptation scheme one step further, perfect solution to the problem of fault tolerance mechanism, make the development more efficient.

Are there any downsides to this plan?

Of course, there are, first of all, the UI adaptation framework has stopped adaptation, which also means that once the framework in the process of using difficult to solve the problem, replacing it will be very troublesome and energy consumption, of course, if you are already familiar with the source except ha.

4. Sw qualifier adaptation scheme

Sw qualifier adaptation is called smallestWidth adaptation, also known as minimum width qualifier adaptation, which refers to the minimum dp value that Android will recognize to the available height or width of the screen. For a phone, this value is actually the width value of the phone, and for a tablet, this value is the height value of the tablet. Then according to the result identified to find the resource file in the corresponding qualifier folder of the resource file. In fact, this adaptation scheme is the same as the previous introduction of the width and height qualifier adaptation scheme principle, is the system through specific rules to find the corresponding resource file.

Let’s first take a look at the resource file, which is based on 375DP generated by the plug-in. The use of the plug-in will be introduced later, as shown in the figure:

For example: For example, I have a xiaomi test machine with a resolution of 1080×1920 and a screen pixel density of 480. According to the minimum width qualifier adaptation scheme, the minimum width of the phone is 1080px, and according to px= DP (DPI /160), the horizontal DP value is 1080/(480/160), i.e. 360DP. Based on this result, the system will look for the resource file in the values-sw360dp folder, as shown in the figure:

As mentioned above, the principle of this scheme is the same as that of the width and height qualifier adaptation scheme, so we also need to set the base size in advance. For example, I use 375dp as the base, that is, the width of any resolution is divided into 375. Let’s take a look at the base resource file, as shown in the figure:

Let’s take the above Xiaomi phone as an example. Let’s take a look at the resource file under the values-SW360DP folder, as shown in the picture:

Dp_1 =(360/ base)*1=(360/375)*1=0.96dp

Dp_2 = (360 / benchmark) * 2 = (360/375) * 2 = 1.92 dp

.

Dp_375 = (360 / benchmark) * 375 = (360/375) * 375 = 360 dp

The same is true for other phone sizes.

So how is the resource file generated?

Implementation steps:

1. Install ScreenMatch in Android Studio

Click File -> Settings -> Plugins on the menu bar, search for the keyword ScreenMatch, install the plugin, and then click “OK” to restart Andorid Studio, as shown in the picture below:

2. In the values folder of the project, you need a dimens. XML file, which is the default base size file mentioned above, as shown below:

3. After the plug-in is installed, right-click on any directory or file of the project and select ScreenMatch, as shown in the picture:

4. Then select the module that needs to be adapted. Here I test only one module, as shown in the figure:

5. After clicking OK, the following popup window will appear, indicating that the resource file is successfully generated, as shown in the figure:

By following the steps above, all resource files of default size are generated in the res directory.

The minimum width reference value of the above steps is 375DP, and the minimum width of the suitable equipment is: 5 generated file, 240320384392400410411480533592600640662720768800811820960961102, 4128, 0136, of course, the actual development needs to be set according to design demand, At this time you need to change the baseline value and need to adapt to the adaptation, then look at how to change.

For example, if the minimum width of the UI design drawing is 360DP, you need to change the minimum width to 360DP. Minimum width if we need to fit the equipment keep 320360384392400410411480 can, if you still need to add other minimum width of the equipment on their own and can, first of all, we need to find the plug-in screenMatch. The properties and open the configuration file, As shown in the figure:

Then we need to modify the configuration file by modifying the values 1, 3 and 4 in the following figure, as shown in the figure:

Note that the values set in the figure above are all in dp.

1, represents the minimum width reference value, fill in the minimum width of the design drawing. 2. Represents the minimum width value that the plug-in ADAPTS by default, that is, the dimens.xml file with the following values is generated by default. 3. Represents the minimum width value that needs to be adapted, i.e. which dimens.xml files you want to generate. 4. Ignore the minimum width value that does not need to be adapted, that is, ignore the dimens.xml file generated by the plug-in by default.

After modifying the configuration file, perform Step 3 again to generate a new resource file.

The main difference between sw qualifier adaptation and width and height qualifier adaptation is: The former has a good fault tolerance mechanism. If there is no value- SW350DP folder, the system will look down. For example, if the nearest value- SW350DP folder is only value- SW350DP, Android will select the resource file under value- SW350DP folder. This feature perfectly solves the fault tolerance problem of wide and high qualifier adaptation schemes. And the SW qualifier adaptation scheme is very stable, very low probability of accidents. With plug-ins, the cost of learning is also low.

Are there any downsides to this plan? Of course there are

  • The most obvious is that it takes up a lot of resources, which will increase the volume of APK
  • The way dimens is referenced in the layout can be cumbersome to change during maintenance.
  • It is highly intrusive. For example, if you want to switch other adaptation schemes, due to the large number of Dimens references in each layout file, the workload of modification will be huge and the switching cost will be very high.

In order to avoid too long, here is the introduction of the two adaptation solutions, the following several will be analyzed one by one, please look forward to. Welcome to pay attention to the public account [Dragon Xuan] can get the latest updates oh.