This article is mainly about the use of length units (DP, PX, SP) in Android.
1, px:pixels (pixels)
(1), different devices different display display effect is the same
(2), this is the absolute pixel, is how much will always be how much will not change.
In general, we describe the resolution of the screen, such as resolution 800*480, the actual representation of the mobile phone screen at a height of 800 pixels, the width of 400 pixels, the entire screen is composed of 800*480 pixels. But because our screen size is different, it causes the same resolution to behave differently on different sizes of screens, and the more pixels are distributed on the screen at the same resolution, the smaller the size, the higher the resolution, the clearer the screen.
2. dpi:dpi (dots per inch)
Indicates the number of dots per foot (pixels)
We usually say that the size of the phone screen, such as 4.5 inches, refers to the length of the screen diagonal is 4.5 inches, then our DPI calculation formula is:
DPI (dots per inch) =/size
DPI represents pixels per foot, and the higher the DPI, the higher the screen granularity, the clearer it is.
3. DP:DP (Device independent pixels)
Very simple, and the dip is the same.
Conversion formula for DP and PX: px=dp* (dpi/160)
Example: On the same size 2 screen, assuming that the resolution is 480*320, 960*640, calculated 480*320 DPI is 160, assume that there is a TextView control on the screen, respectively set its length (layout_weight) is 160px, 160DP, The performance of the two is drawn.
Analysis Process:
Because the screen size is the same, the resolution is 480*320, 960*640, where the dpi of 480*320 is 160, then the DPI of the other screen is 320, the result is as follows:
When there is a TextView control on the screen, set its length (layout_weight) to 160px, 160DP respectively, then draw the performance of both
Principle:
The first screen dpi is dpi=160, the second screen of the dpi=320, if you apply the above DP and PX conversion formula can be obtained:
The screen resolution is 480*320, the PX of this control is: 160px=160dp* (160/160), that is, 160px, which accounts for half of the screen
160dp=160px
160px=160px
The screen resolution is 960*640, the px of this space is: 320px=160dp* (320/160), that is, 320px, also accounted for half of the screen
160dp=320px
160px=160px
This way our application can show the same effect at different resolutions.
PS: When you want to set the width of the control or the size of the height, the unit to be used is DP, instead of using PX.
4, sp:sp (scaled pixels)
SP This unit usually we use to set the size of the font
If we change the font size of the system, the display size of its controls will change as well.
5. Get screen resolution and density
1 New displaymetrics (); 2 Getwindowmanager (). Getdefaultdisplay (). Getmetrics (displaymetrics); 3 System.out.println ("width:" + displaymetrics.widthpixels + " Height:" + displaymetrics.heightpixels + " density:" + displaymetrics.density);
Finally in Logcat we can see the values of width, height, density
When Density=1, then 1dp=1px;
When density=2, then 1dp=2px;
When density=3, then 1dp=3px;
APK in the resource bundle,
When the screen density=240, that is, 1dp=1.5px, the resource using the hdpi tag
When the screen density=160, that is, 1dp=1px, the resource using the MDPI tag
When the screen density=120, that is, 1dp=0.75px, the resource using the ldpi tag.