Last article the screen adaptation related to the concept of sorting out, mainly in order to better understand the content behind, that from the beginning of this is to explain adaptation plan, did not see the first can see this article: Android comprehensive screen adaptation plan analysis (a)

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

See this some children’s shoes may be asked, why some outdated adaptation scheme here also explained ah, can only say that each adaptation scheme will have their own advantages and disadvantages, from the original adaptation scheme, to better understand why will derive a variety of adaptation scheme, not to say more, directly open to do it!

1. Dp adaptation scheme

As we all know, Android does not recommend using the real pixel unit PX when marking the size in the development process, because the same size in different resolutions of the mobile phone display effect is different, as shown in the following image width and height 250×250(px), in the resolution: 480×800, 720×1280, 1080×1920

With an effect like the one above, the overall layout can be distorted, so the px unit is not recommended in layout files.

The corresponding Android recommended using DP as the size unit to adapt UI. As mentioned before, DP is a density-independent pixel, which has nothing to do with the actual physical pixels on the terminal, so that the same effect can be displayed on devices with different screen pixel densities.

The following is an example of the image with a width and height of 250×250(DP) and a resolution of 480×800, 720×1280 and 1080×1920:

As can be seen from the renderings, the overall layout effect of 250DP with different resolutions is not very different. Why is this?

We know that for devices of the same size with different resolutions, the number of pixels represented by each 1DP is different, as shown in the table:

As can be seen from the table, dPI is different in 480×800, 720×1280 and 1080×1920 mobile phones, which means that 1DP corresponds to 1.5px, 2px and 3px respectively in these mobile phones with different resolutions. Thus, when dp is used as the control size unit, If you see the same size on different resolutions, you’ll see the same scale on all phones.

The above adaptation method, dp plus adaptive layout and weight proportional layout can basically solve the problem of adaptation on different mobile phones, which is basically the most original Android adaptation scheme.

Are there any downsides to this plan?

Naturally, there are, otherwise it would not have launched so many adaptation programs, then let’s see what the shortcomings are.

We know that the fragmentation of Android is so serious because of the fragmentation of Android system, screen size and screen density. Moreover, the mobile devices produced by mobile phone manufacturers are not implemented according to the relation rules of screen size, resolution and pixel density. For example, the screen resolution is 1080×1920 and the screen size is 5. If the DIP is 440, what would be the problem if the UI design was designed with a screen width of 375DP?

According to the above description, we can calculate the screen width as: 1080/(440/160) = 393DP, that is to say, the actual screen width is wider than the design drawing, so even if dp is used as a unit, it cannot display the same effect as other devices, which requires conversion Settings by estimating or setting standard values, etc., which requires us to spend energy to convert the size. This can greatly reduce development efficiency.

2. Width and height qualifier adaptation scheme

The so-called width and height qualifier adaptation is a summary of the width and height pixel values of all Android phones on the market, but you need to set a baseline and then adjust the other resolutions according to this baseline, as shown in the figure below:

So what do you mean by setting a benchmark?

For example, if 320×480 resolution is set as the benchmark, then:

The base width is 320, that is, the width of any resolution is divided into 320 parts, ranging from X1 to X320.

The base length is 480, that is, the length of any resolution is divided into 480 parts, ranging from Y1 to Y480.

Then the dimens file corresponding to the baseline size is written as:

So what does that mean that other resolutions are adapted to this benchmark?

For example, for a phone with a resolution of 480×800, you need to set the following Settings in the dimens. XML file under the values-800×480 directory in the project, as shown in the figure:

So how is this data calculated? That is, of course, based on the baseline resolution. The following is the width X demonstration:

X1 = (480 / benchmark) * 1 = (480/320) * 1 = 1.5 px

X2 = (480 / benchmark) * 2 = (480/320) * 2 = 3 px


480 x320 = (/ benchmark) * 320 = (480/320) * 320 = 480 px

And the same thing is true for length y.

At this time, there are children shoes said, how do I know how many resolutions of the mobile phone on the market, even if I know all the resolutions, each such calculation shall not write waste ah, calm down ha, these are automatically generated by the tool, which is thanks to the generation tool provided by hongyang.

1. Analyze the resolution required for support

For the mainstream resolution has been integrated into the program, for the more special can be specified by parameter, and for screen resolution information, can be queried through the website: click here to jump to

2, automatically generate file program address

Click here to jump to the autogenerator

Here is a jar package that can be generated by double clicking by default, as shown below:

The default resolution is 480*320. For special requirements, you can specify it through the command line. For example, I want to use 1280 * 800 as the base resolution and support additional dimensions: 1152 * 735; 4500 * 3200, as shown in the figure:

The format is as follows:

java -jar xx.jar width height width,height_width,height
Copy the code

Finally, the auto-generated file is shown in the figure:

This changes the default baseline and adds additional supported resolutions.

Can use this adapter, according to the size of the UI design draft for reference resolution, running on different resolution in the mobile phone at this moment, these systems will be based on these dimens reference to the folder below to find the corresponding value of the resolution, so basically settled the problem of our adapter, and greatly improve the efficiency of our UI development.

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

  • The most obvious affirmation is the large footprint, which will increase the size of APK
  • For example, a 1920×1080 phone must find the qualifier of 1920×1080, otherwise it will have to use the uniform default Dimens file. With the default size, the UI is likely to be distorted.

In order to prevent the length is too long, here is the introduction of two adaptation schemes, after several will be analyzed one by one, please look forward to, welcome to pay attention to the public number [Dragon xuan] can get the latest updates oh.