Why talk about screen adaptation?

The official recommendation of development process has been used, dip, occasionally seen in dimens file adapter, behind also saw some advice dip is enough, dimens file adapter has a pit, he hadn’t tried, lazy, developed to a few test machine at ordinary times, at times, in different screen adaptation looks not very comfortable, Although I probably know that the experience is inconsistent due to the same pixel in different sizes, I have been too lazy to change, and I don’t know how many people are like me. , until… .

Until recently, I suddenly wanted to develop a TV project, but there was a big gap between the real computer simulator and the dip method. After using Dimens adaptation, there was basically no difference between the effect and the design drawing, but I still didn’t go into the details, until last week, I was asked how the Android screen ADAPTS.

How did you adapt to the project?

The official DIP is not recommended because of the difference in the width and height of the dip due to different sizes of pixels on different screens. Now there are popular AutoLayout and Dimens adaption. There are too many AutoLayout issues, and the author doesn't seem to update them any more.Copy the code

2 Where did the dimens file come from?

Generated by the tool.Copy the code

Do you know the principle of this tool? . .

I was surprised to ask this question. After reading the Nuggets and some blogs, I know that screen adaptation is very popular this month. I read the article several times and retell some key questions in my own words to deepen my understanding.

Root cause of inconsistent adaptation and UI designer using DIP?

I saw the description

Density is fixed on each device, DPI / 160 = density, the total px width of the screen/density = the total DP width of the screen

Device 1, screen width 1080px, 480DPI, total screen DP width 1080 / (480/160) = 360DP Device 2, screen width 1440,560 DPI, The total screen DP width is 1440 / (560/160) = 411DP

If the same DIP is used in different screens, the results will be different. For example, the dip width of 100DIP is 0.278 in the dip width of 360, and 0.243 in the dip width of 411DIP. So it’s going to be different on different screens

Principle of headline adaptation

According to the formula dp * density = px, and dp has been fixed, the value of PX can be obtained by modifying density. How does density come from? The end result should be to assume that the dip is uiDip width px uPxWidth and set the DIP to deviceDip width dPxWidth

The dPxwidth and deviceDip of the screen have been fixed and density is calculated based on the device width PX and the dip of the design drawing

density = devicePxWidth / uiWidthDip

According to the formula, the DIP width of the design drawing is calculated as the PX width of the equipment, and the final width ratio is consistent with the width ratio in the actual drawing