To solve Android device fragmentation, describe the concept of density, that is, density. it refers to the number of pixels displayed on a physical screen of a certain size. dpi (dots per inch, number of pixels per inch) is generally used as a single
Bit. for example, if the resolution of a device is 240x320 and the physical size of the screen is 1.5 inch x 2 inch (the diagonal line is 2.5), the density of the device can be expressed as 240/1. 5 or 320/2, with a density of 160 dpi. The same is true for diagonal lines.
The square of the Length + the square of the width = 160000, and the square after the opening = 400. 400/2. 5 = 160. for users, the more dense the screen display effect, the more precise the display, because the number of pixels per inch is more.
The above description is a blueprint (which corresponds to mdpi ).
A dp is equivalent to a px in the 160dpi screen. Similarly, a dp is equivalent to two px in the 320dpi screen.
Android solves the problem of requiring multiple Display sizes on different screens.
So in the Eclipse layout file, take 3.7in WVGA (Nexus One) (480x800) as an example, we can see it as sw320dp, h533dp.
That is, the width is 320dp, and the height is 533dp.
How is it calculated?
Opening (480 square + 800 square) = 932.9523031
Then the screen size is 932.9523031/3.7 = 252.14927. The result is the screen density of the mobile phone.
Then, based on the above blueprint, we should calculate the amount of px for 1dp on the mobile phone. The calculation is as follows:
160 1dp = 1px
252.14927 1dp =? Px
Result:
252.14927/160 = 1.5759329
Note:
Generally, 1.5759329 is not used here. Only 1.5 is used.
Therefore: 480/1. 5 = 320; 800/1. 5 = 533.3333 (usually 533), that is, the above sw320dp, h533dp.
Phone screen Density
120 ldpi
160 the up and down float belongs to the mdpi
240 float up and down all belong to hdpi
320 both the upstream and downstream devices belong to xdpi.
Taking Motorola ME525 + (3.7 inch resolution 854x480 pixel) as an example:
Opening (480 square + 854 square) = 979.6509
Then the screen size is 979.6509/3.7 = 264.770529. The result is the screen density of the mobile phone, which corresponds to hdpi.
264.770529/160 = 1.6548
That is, 1dp = 1.6548 pixels in this phone, only 1.6.
So: 480/1. 6 = 300; 854/1. 6 = 533.75 (usually 533)
According to this logic, the calculated width should be only 300 DP. The tragedy is that if the width of a widget is, it cannot be filled with the width of the mobile phone.
That is to say, the difference is not 1.6, but 1.5 !!!! So set the width to 320.
Summary: In hdpi, pixel conversion to dp is unified using: pixel/1.5
Take Lenovo K860 (5.0 inch, resolution X pixels) as an example:
Opening (1280 square + 720 square) = 1468.60478
Then the screen size is 1468.60478/5 = 293.720. The result is the screen density of the mobile phone, which corresponds to xdpi.
293.720/160 = 1.8357
That is, 1dp = 1.8357 pixels in this phone
So: 1280/1. 8357 = 697.281; 720/1. 8357 = 392.22
According to this logic, the calculated width should be 392. 22. The tragedy is that if the width of a widget is 392.22, it is not completely displayed on this phone.
That is to say, the dividing is not 1.8, but 2 !!!! So set the width to 360.
Summary: In xdpi, pixel conversion to dp is unified using: pixel/2
Note Question 1:
Note when creating a simulator
In Hardware, set the density of the 854xlarge LCD to 240 and enter the resolution manually.
1280x720 in Hardware, you should set the effecacted LCD density to 320 and manually enter the resolution
Note Question 2:
Multiple layout folders have corresponding naming rules, take resolution 480x854 as an example, need to create a 480x854 pixel layout folder, name: layout-854x480, there are two points need to note: ① large number (854) must be in front,
Otherwise, an error is reported. ② the symbol between two numbers is lowercase English letter "x", not a multiplication number.