Px, dp, sp, and androidpxdpsp used in android Development

Source: Internet
Author: User

Px, dp, sp, and androidpxdpsp 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.
[Programming] [Android] [development]/what does dip dp px sp mean? What are the recommended locations?

Display Unit: pixel, dip, and sp

Dip: device independent pixels (device independent pixel ). different devices have different display effects, which are related to the hardware of the device. We recommend that you use this function to support WVGA, HVGA, and QVGA without pixels.

Px: pixels (pixels). Different devices have the same display effect. Generally, we use HVGA to represent 320x480 pixels, which is usually used.

Pt: point, a standard unit of length, 1pt = 1/72 inch, used in the printing industry, very easy to use;

Sp: scaled pixels (zoom in pixels). It is mainly used to display the best for textsize in fonts.

Therefore, according to google's suggestion, it is best to use sp as the unit for TextView font size and view
TextView
Android uses sp as the font size unit by default.

######################################## ###########################

Conversion (using sp and pt as an example)
View the source code of TextView and other classes. We can see that:

Case COMPLEX_UNIT_PX:
Return value;
Case COMPLEX_UNIT_SP:
Return value * metrics. scaledDensity;
Case COMPLEX_UNIT_PT:
Return value * metrics. xdpi * (1.0f/72 );

--------------------------
ScaledDensity = DENSITY_DEVICE/(float) DENSITY_DEFAULT;
Xdpi = DENSITY_DEVICE;

--------------------------
DENSITY_DEFAULT = DENSITY_MEDIUM = 160;

========================================================== ====
Therefore, if pt and sp take the same value of 1, the coefficient between 1pt and 1sp can be set to x,

1 * DENSITY_DEVICE/72 = x * 1 * DENSITY_DEVICE/160 =>
X = 160/72 = 2.2222

That is to say, in Android, 1pt is roughly equal to 2.22sp

The above is for reference. If the UI can provide the best design in sp units, if there is no sp in the Design
Then, developers can take an approximate value by appropriate conversion.

What are Dip and Sp?

In the past, programmers often designed computer user interfaces in pixels. For example, define a form field with a width of 300 pixels. the spacing between columns is 5 pixels, And the Icon size is 16 × 16 pixels. The problem with this processing is that if you run the program on a new display with a dot (dpi) higher per inch, the user interface will look small. In some cases, the user interface may be small to difficult to see the content.

The measurement units irrelevant to resolution can solve this problem. Android supports all of the following units.

Px (pixel): the point on the screen.

In (INCHES): the unit of length.

Mm (mm): the unit of length.

Pt (lbs): 1/72 inch.

Dp (density-independent pixels): An abstract unit based on screen density. 1dp = 1px on a display at 160 o'clock per inch.

Dip: it is the same as dp. It is mostly used for Android ...... the remaining full text>

In Android, how does one convert between dp and px?

Px, in, mm, pt, dp, dip, and sp are often used in Android development. Unit px: The pixel of the screen in: inches mm: mm pt: lb, 1/72 inch dp: an abstract unit based on density. If a screen of 160dpi is used, 1dp = 1 pxdip: equivalent to dpsp: similar to dp, but it also scales according to the user's font size preference.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.