Size Units (px, dpi, dp, dip, and sp) of the View for android Application Development, and androiddpi
I. pixel (px) and screen resolution
1) px (Pixels, Pixels): corresponds to the actual Pixels on the screen. For example, 320*480 screens have 320 pixels horizontally and 480 pixels vertically.
2) the screen resolution is the total number of physical pixels displayed on the screen. It is equal to the number of pixels x height on the screen width. In the development of android applications, screen resolution is not used.
3) Although the screen resolution is wide * high, it has nothing to do with the screen size, and it is related to the number of pixels on a screen.
Ii. dpi
1) dpi: pixels per inch, which is the unit of screen pixel (px) density. The pixel density is the number of pixels that can be displayed within the specified physical width and height range on the screen.
2) In android, dpi is classified as follows: ldpi, mdpi, hdpi, xhdpi, xxhdpi, and xxxhdpi.
3) In reality, mdpi is usually used as a benchmark, which indicates 160 dpi and defines this density as a unit of density. 160 DPI indicates that there are 160 * pixels in an area of 1 square inch. Ldpi is 120 dpi, hdpi is 240 dpi, xhdpi is 320 dpi, xxhdpi is 480 dpi, xxxhdpi is 640 dpi, that is, ldpi density is 0.75, the hdpi density is 1.5, xhdpi is 2, xxdpi is 3, and xxxhdpi is 4.
4) dpi is independent of the screen size. It is only a measure of pixel density.
Iii. dp
Independent of the physical density measurement unit, it is based on 160dpi, regardless of the dpi screen, the physical length expressed by 1 DP is the same. On a 1.5 DPI screen, 1dp = 1px, that is, a dp needs to be represented by a px. On a DPI screen, 1dp = 1.5px, indicating that a dp requires px. That is, 1dp = dpi/160 * px. It enables developers to develop a UI with the same display size regardless of the dpi blocking. That is to say, on a cell phone with the same size but different resolutions, what is identified by dp is displayed in the same size.
Iv. dip
It is the same as dp, but the name is different.
V. sp
The abbreviation of scaled pixels to enlarge pixels, which is used to process the font size. It is related not only to the screen dpi, but also to the default font size of the system. For the same sp value, the larger the screen dpi, the smaller the displayed font. The larger the system font, the larger the displayed font. This is why all fonts in sp units have changed after we set the system font size.
If the font is in the unit of dp, the system font size is modified, and the font size of the View on the interface is not changed.
For example, for the following code, two textviews are in a vertical distribution of LinearLayout, the first TextView uses the sp as the font unit, and the second TextView uses the dp as the font unit:
<TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="28sp" /><TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="28dp" />
In the mdpi device, the system font is hour, as shown below:
The two methods have the same font size. If the system font is normal on an hdpi device, the effect is the same.
When the font size is set to ultra-large, the effect is as follows:
We can see that the font size for using dp has not changed, and the font for using sp has obviously increased.