To solve the Android device fragmentation, introduce a concept density, that is, density. It refers to the number of pixels displayed on a physical screen of a certain size, generally using the dpi (dots per inch, pixel of each inch) as a single
such as the device resolution of 240x320, screen physical size of 1.5 inches x2 inches (Diagonal 2.5) Its density can be expressed in resolution/size: 240/1.5 or 320/2, density 160dpi. It's the same as the diagonal.
, the square of long square + width is 160000, after =400.400/2.5=160. For users, the greater the density of the screen display the more sophisticated, because the number of pixels per inch display more.
The above description is a blueprint (this blueprint corresponds to MDPI).
According to this blueprint a DP is equivalent to a PX in a 160dpi screen. Similarly, in the 320DPI screen, a DP is equivalent to 2 px.
With this proportional scaling up, Android solves the need for size display problems in multiple different screens.
So in Eclipse's layout file, take 3.7in WVGA (Nexus One) (480x800) as an example, you can see that it is SW320DP,H533DP.
That is 320DP wide and high to 533DP.
How does it come to be calculated?
=932.9523031 (square +800 of 480 squared)
Then 932.9523031/Screen size 3.7=252.14927, this result is the screen density of the phone.
Then according to the above blueprint, it should be calculated on the phone 1DP for how many px. The calculation is as follows:
160 1dp=1px
252.14927 1dp=?px
The results are:
252.14927/160=1.5759329
Attention:
This generally does not take 1.5759329, only to 1.5 can be.
So: 480/1.5=320;800/1.5=533.3333 (usually 533), that is, the above SW320DP,H533DP.
The corresponding relation of cell phone screen density
120 of the floating are belong to LDPI
160 of the floating are belong to MDPI
240 of the floating are belong to HDPI
320 of the floating are belong to XDPI
Take the Motorola me525+ (3.7-inch resolution 854x480 pixel) for example:
=979.6509 (square +854 of 480 squared)
Then 979.6509/Screen size 3.7=264.770529, this result is the cell phone's screen density. Corresponds to hdpi
264.770529/160=1.6548
That is, 1dp=1.6548 a pixel in this phone, only to 1.6
So: 480/1.6=300;854/1.6=533.75 (usually taken as 533)
According to this logic, the width of the calculation should be only 300DP. Sadly, if a control is 300 wide, it cannot be covered with a wide cell phone.
Which means it's not divided by 1.6 or 1.5!!!! So the width is set to 320.
Summary: In hdpi, pixels are converted to DP unification using pixels/1.5
Take Lenovo K860 (5.0 inch, resolution 1280x720 pixel) as an example:
=1468.60478 (square +720 of 1280 squared)
Then 1468.60478/Screen size 5=293.720, this result is the cell phone's screen density. Corresponds to xdpi
293.720/160=1.8357
1dp=1.8357 a pixel in this phone
So: 1280/1.8357=697.281;720/1.8357=392.22
According to this logic, the width should be 392.22. The tragedy is that if a control is 392.22 wide, it appears incomplete on this phone.
In other words, it's not 1.8 but 2!!!! So the width is set to 360.
Summary: In xdpi, pixels are converted to DP unification using pixels/2
Note Question 1:
When setting up the simulator, be aware
854x480 in hardware, you should set the abstracted LCD density to 240 and manually enter the resolution
1280x720 in hardware, you should set the abstracted LCD density to 320 and manually enter the resolution
Note Question 2:
Multiple layout folders are named with the corresponding rules, in the case of resolution 480x854, you need to establish 480x854 pixel layout folder, named: layout-854x480, there are two points to note: ① large number (854) must be in front,
Otherwise there will be an error; ② the symbol between the two digits is the lowercase English letter "x", not the multiplication