Px, dp, and sp used in android Development
First, we will introduce these units:
Px: pixels (pixels), which corresponds to the actual pixels on the screen.
Dip: device independent pixels, which is a density-independent pixel. It is an abstract unit based on screen density. On a display at 160 o'clock per inch,
1dp = 1px, that is, the 1: 1 relationship. (Dp is dip)
Sp: scaled pixels (enlarged pixels). pixels irrelevant to density and font scaling. It is mainly used to display the best for textsize in the font.
The screen of android devices is messy, and the width and height of various styles are not good. After a set of layout is developed, the painful thing is that the screen is adapted.
How can we ensure that layout can be normally displayed on screens of different resolutions? Next we will give a brief analysis of some common units in android.
Px is the unit used by the UI Designer to cut the image in PS. This unit is also available on the mobile screen.
Dp is the size unit used for layout development in android.
Sp is the font size unit of layout. When the font unit is set to sp, the font can be automatically scaled according to the screen size.
Android supports five different dpi modes: ldpi, mdpi, hdpi, xhdpi, and xxhdpi.
Generally, the resolution of the phone matches the dpi mode, that is, different dpi folders under Res are loaded according to the size of the screen.
For example, the HVGA (3.5 inch x pixels) mobile phone screen is usually around, and runs in mdpi mode (with exceptions from the wonderful camera)
(This is ROM-controlled, and the app cannot be changed ). When running in mdpi, 1dp = 1px: that is to say, the designer defines an item with a height of 48px in PS. during development, this item is defined with a height of 48dp. In Photoshop, the font with a size of 14px is defined, it is defined as 14sp during development.
For a WVGA (480x800) mobile phone, it generally runs in hdpi mode. When running in hdpi mode,
1dp = 1.5px: that is to say, when the designer defines an item with a height of 72px in PS, it will define the item with a height of 48dp during development;
The 21px font in Photoshop is defined as 14sp by developers.
Therefore, when your app needs to adapt to multiple dpi modes (such as mdpi and hdpi at the same time), If you design (or higher) in WVGA ), you must set each value to a multiple of 3,
All the numbers (/3*2) are converted to dp during graph tagging, so that the same layout can be used in two different dpi modes, instead of writing two layout sets.
Proportion:
The relationship between mdpi and hdpi is
The relationship between mdpi and xhdpi is.
The relationship between ldpi and mdpi is
For example, my machine is Red Rice and the screen size of 720*1280 is running under xhdpi.
One exception is that some of the more sophisticated Pad may be a 7 inch screen, and the resolution is WVGA (480*800) running under the mdpi.