Unit conversion between DIP (DP) and PX in Android

Source: Internet
Author: User

PX: Is the pixel point of the screen
In: Inch
MM: MM
PT: lbs, 1/72 inch
DP: An abstract unit based on density, if a 160dpi screen, 1dp=1px
Dip: Equivalent to DP
SP: similar to DP, but also scaled according to the user's font size preference.
It is recommended to use SP as the unit of text, other dip

For the dip and PX relationship, do the following overview:

QVGA screen density=120; QVGA (240*320)

HVGA screen density=160; HVGA (320*480)

WVGA screen density=240; WVGA (480*800)

WQVGA screen density=120 WQVGA (240*400)

The density value represents the number of display points per inch, and the resolution is two concepts.

Screen resolution information under different density, with 480dip*800dip WVGA (density=240) as an example

When density=120

Screen actual resolution is 240px*400px (two points corresponds to a resolution)

Status bar and title bar height 19px or 25dip

Screen width 400px or 800dip, working area height 211px or 480dip

Screen width 240px or 480dip when vertical screen, working area height 381px or 775dip

When density=160

Screen actual resolution is 320px*533px (3 points corresponds to two resolutions)

Status bar and title bar tall 25px or 25dip

Screen width 533px or 800dip, working area height 295px or 480dip

Screen width 320px or 480dip when vertical screen, working area height 508px or 775dip

When density=240

Screen actual resolution is 480px*800px (one point for one resolution)

Status bar and title bar tall 38px or 25dip

Screen width 800px or 800dip, working area height 442px or 480dip

Screen width 480px or 480dip when vertical screen, working area height 762px or 775dip

APK in the resource bundle

Resources that use HDPI tags when the screen is density=240

Resources that use MDPI tags when the screen is density=160

When the screen density=120, use the ldpi tag resource.

Resources that are not tagged are shared in a variety of resolution scenarios.

Use unit dip as much as possible in layout, use less px

DP and PX Conversion formula:

Pixs =dips * (densitydpi/160).

Dips= (pixs*160)/densitydpi

DP This unit may be unfamiliar to people who are web developers, as they are generally using px (pixels)
But now, after starting Android apps and games, it's basically switching to DP action, because it can support multiple resolutions of the phone.


The following are the concepts of these two units:
PX (pixels) pixels – a pixel is usually considered as the smallest complete sample of an image, which is used more often, especially in web development, where the page basically uses pixels as the unit.
Dip or DP (device independent pixels) devices independent pixels-this is related to device hardware and generally we use dip as a unit of length to support multiple resolutions on the phone, such as WVGA, HVGA, and QVGA
In Android development we can generally do not need to use PX, but some of the properties of the control does not directly support dip, like the following code
Android.view.ViewGroup.LayoutParams.height
Android.view.ViewGroup.LayoutParams.width

The above two properties are in pixels, but in order to be compatible with a variety of resolutions, we need the best dip, and we can call the following code to convert it.
int heightpx= displayutil.dip2px (this, 33);
Mtabhost.gettabwidget (). Getchildat (i). Getlayoutparams (). height = heightpx;
The above code can be seen in another article. The function is to set the tab height, in pixels. The above units are converted to support multi-resolution phones.

public static int dip2px (context context, float Dpvalue) {
Final float scale = context.getresources (). Getdisplaymetrics (). density;
return (int) (Dpvalue * scale + 0.5f);
}

public static int Px2dip (context context, float Pxvalue) {
Final float scale = context.getresources (). Getdisplaymetrics (). density;
return (int) (Pxvalue/scale + 0.5f);
}

Unit conversion between DIP (DP) and PX in Android

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.