sw screen adaptation

   In Android devices, adaptation is the most common problem. In Android, because of the different screen resolutions, the display of our UI will be very different, so how can we ensure that the same interface is displayed on different screens? Is it the same? This is the adaptation we are going to do today, so what are the adaptations? The traditional dp is actually a kind of adaptation, and then there are adaptations written by Hongyang Great God himself, but since it has not been maintained for a long time, it seems that no one uses it anymore. So what are the commonly used adaptations now? Adaptation two, sw adaptation, headline adaptation may be familiar to everyone, so today we will talk about how to implement SW adaptation

        The original name of sw adaptation is smallestWidth, so how to implement it elegantly and conveniently, let us take a look now.

1. Introduction

The way of using this scheme is no different when we quote the dimens file in the layout. The core point is to generate dimens files with different width qualifiers, as shown in the figure below (how to generate the detailed steps below)

Second, the realization principle

First, in the project, according to the minimum width of the mainstream screen (smallestWidth, here is how to calculate this value: first step: calculate the DPI of the screen, here is 1920*1080 screen size 5.2 to calculate DPI=1080*1080+1920*1920 and then Square root and divide by 160=2.6477; the second step is to calculate the minimum width: 1080/2.6477=408) Generate a series of values-sw<N>dp folders, when the project is run on different devices, the system will follow The current minimum width is set to match the corresponding dimens file in the corresponding folder. If the system does not find the corresponding folder according to the current minimum width of the screen, it will find the folder with the minimum width of the current device, that is, it will only look for the folder with the minimum width of the current device. Or a folder equal to the minimum width of the current device. For example, the minimum width of the current device is 502. The folder generated at that time does not contain 502 folder, but there are 500 and 503 folders, then the system will match the 500 folder . This is a fault-tolerant mechanism that is superior to the width and height qualifier screen adaptation scheme, and it can generate a lot of folders and reduce the size of the apk.

 

Three, the principle of dimens.xml generation

Two factors are mainly involved. The first is to determine the minimum width reference value. The second factor is to determine which minimum width the project needs to adapt to. The popular understanding is how many different dimens files need to be generated.

The first factor: The minimum width reference value can be understood as how many parts you want to divide the screen into. Assuming that the screen is now divided into 360 parts, the solution will generate a dimens reference from 1 to 360 in the dimens file, as shown in the figure below (the screenshot benchmark here) The value is 375, so the value of each portion is not enough 1dp, and the width of each portion is 0.96dp)

Here I will take a screenshot of the screenshot of sw-400

In the dimens file of sw-400, the width of one copy is 1.0667, so that the largest dp_360 can just cover the width of the entire screen to achieve adaptation, so as to ensure that no matter which device the project is running on, as long as it matches the corresponding The dimens file can achieve perfect adaptation. Even if it is not matched, it can also be fault-tolerant to the greatest extent. It can also be adapted without major adaptation problems. The principle of this adaptation method is actually Percentage adaptation.

 

The second factor: what minimum width do we need to adapt

Theoretically speaking, the more minimum width files can guarantee the perfection of the adaptation to the greatest extent, but this is not the case in actual operation. First of all, the minimum widths of mainstream models are common ones, and more files may not be Secondly, the more files, the larger the size of the apk file, so you should choose a suitable solution between the two (the most mainstream ones have been implemented in the following plug-ins, which can basically cover all machines. Type, if you find that there is no coverage, you can modify and add in the configuration file, this will be discussed later)

 

Fourth, the advantages of the program

1. Very stable, with a very low probability of accidents

2. There will be no performance loss (or Google’s original dp adaptation)

3. The scope of adaptation can be controlled freely and will not affect third-party libraries (such as Toutiao and Hongyang's autoLayout will have this problem)

4. With the cooperation of plug-ins, the scheme is easy to transplant and the cost is very low

 

Five, the shortcomings of the program

1. Introduce the dimens file in the layout. Although the learning cost is low, it will be a little troublesome to maintain and modify

2. Highly invasive, same as the first one, because a large number of dimens files are introduced in the layout file, which is troublesome to modify

3. Cannot cover all models (personally think this shortcoming can be ignored, no one adaptation scheme can be perfect)

4. Can not automatically support horizontal and vertical screen switching. When the direction is rotated, the width and height will be interchanged. Because there is a minimum limit, the width and height are actually not distinguished. To achieve this, you can add qualifiers such as land and port

 

6. How to use (here, take the 375-width design drawing, the common Apple design is twice the size of the design drawing)

1. Import the plug-in first (as shown below, how to import the plug-in will not be explained)

ScreenMatch.jar

2. Create a dimens file in the values ​​folder of the project and import the default dimens reference values ​​(this step is actually the original dimens adaptation of Android, as shown below)

3. Right-click anywhere in the project to select the red box option as shown in the figure below, and then select the project you want to adapt

 

Click OK to generate different dimens files at the beginning of the article

4. Modify the configuration file according to the actual situation of your own project

Switch to the project directory, open the following red box and check the file, modify the following three places, the first base_dp is the base width of your project design drawing, fill it in according to your actual situation; the second mach_dp is what you want to adapt Some minimum widths; the third ignore_dp ignores the minimum width that does not fit

5. After modifying the configuration file, restart the ide to take effect





 

Guess you like

Origin blog.csdn.net/guodashen007/article/details/108140271