Unit conversion between dp and px in Android
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 DPI is displayed, 1dp = 1px.
Dip: equivalent to dp
Sp: similar to dp, but it also scales according to the user's font size preference.
We recommend that you use sp as the unit of text, and dip for others.
The following is an overview of the relationship between dip and px:
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 value of density indicates the number of display points per inch, and resolution.
For details about the screen resolution of different density, take the WVGA (density = 240) of 480dip * 800dip as an example.
Density = 120
The actual screen resolution is 240px * 400px (two points correspond to one resolution)
The height of the status bar is 19px or 25dip.
The screen width is 400px or 800dip, and the working area height is 211px or 480dip.
Screen width: Px or dip, working area Height: 381px or 775dip
Density = 160
The actual screen resolution is 320px * 533px (three points correspond to two resolutions)
The height of the status bar is 25px or 25dip.
The screen width is 533px or 800dip, and the working area height is 295px or 480dip.
Screen width: 320px or 480dip, working area Height: 508px or 775dip
Density = 240
The actual screen resolution is 480px * 800px (one point for one resolution)
The height of the status bar and title bar is 38px or 25dip.
The screen width is PX or dip, and the working area height is PX or dip.
Screen width 480px or 480dip, working area height 762px or 775dip
In the apk Resource Package
Resources that use the hdpi label when the screen density is 240
Resources that use the mdpi label when the screen density is 160
Ldpi tag resources are used when the screen density is 120.
Resources without any tags are shared in various resolutions.
Use the unit dip as much as possible during layout, and use less px
Formula for converting dp to px:
pixs =dips * (densityDpi/160). dips=(pixs*160)/densityDpi
This unit of dp may be unfamiliar to web developers, because px (pixels) is generally used)
However, after the android app and game are started, it is basically converted to the unit of dp, because it can support mobile phones with multiple resolutions.
The concepts of these two units are as follows:
Px (pixels) pixels-a pixel is usually regarded as the smallest complete sampling of the image. This is usually used, especially for web development. pages are basically measured in pixels.
Dip or dp (device independent pixels) device independent pixels-this is related to the device hardware. Generally, to support multiple resolutions on the mobile phone, such as WVGA, HVGA, and QVGA, dip is used as the unit of length.
In Android development, we generally do not need to use px, but some controls do not directly support dip attributes, as shown in the following code.
android.view.ViewGroup.LayoutParams.heightandroid.view.ViewGroup.LayoutParams.width
The above two attributes are measured in pixels. However, to be compatible with mobile phones with multiple resolutions, we need to use dip. In this case, we can call the following code for conversion.
int heightPx= DisplayUtil.dip2px(this, 33);mTabHost.getTabWidget().getChildAt(i).getLayoutParams().height = heightPx;
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);}