Differences between Android dip, px, pt, and sp

Source: Internet
Author: User

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 ).The display effects of different devices are the same. Generally, HVGA represents x pixels, which is usually used.
Pt: pointIs a standard length unit, 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 the font. According to google's suggestion

It is best to use sp as the unit of font size, and check the source code of TextView. We can see that Android uses sp as the font size unit by default.

In Android, 1pt is about equal to 2.22sp and above for reference. If the UI can provide the best design in sp units, if the design does not have the concept of sp, then, the developer can take an approximate value by appropriate conversion.

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 and is mostly used in android/ophone examples.
Sp (pixels irrelevant to the scale): similar to dp, but can be scaled based on the user's font size preferences.
Resolution: the number of points on the screen, such as 800x480. It is the display unit of the software, in px. The density (density) value indicates the number of display points per inch, and the resolution is two concepts. 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.

Generally, android uses dip to set the length and width, and sp to set the font size. the screen density is 160, 1dp = 1px = 1dip, 1pt = 160/72 sp 1pt = 1/ 72 inch. when the screen density is 240, 1dp = 1dip = 1.5px.
To enable normal display of the current and future display types on the user interface, we recommend that you always use sp as the unit of text size and dip as the unit of other elements. Of course, you can also consider using a vector image instead of a bitmap.

Copy codeThe Code is as follows: import android. content. Context;
Import android. util. DisplayMetrics;
/**
* Formula pixels = dips * (density/160)
*
* @ Version 1.0.1 2010-12-11
*
* @ Author
*/
Public class DensityUtil {
Private static final String TAG = DensityUtil. class. getSimpleName ();
// DensityDpi of the current screen
Private static float dmDensityDpi = 0.0f;
Private static DisplayMetrics dm;
Private static float scale = 0.0f;
/**
*
* Obtain the screen coefficient of the current Mobile Phone Based on the constructor.
*
**/
Public DensityUtil (Context context ){
// Obtain the current screen
Dm = new DisplayMetrics ();
Dm = context. getApplicationContext (). getResources (). getDisplayMetrics ();
// Set DensityDpi
SetDmDensityDpi (dm. densityDpi );
// Density Factor
Scale = getDmDensityDpi ()/160;
Logger. I (TAG, toString ());
}
/**
* Density factor of the current screen
*
* @ Param DmDensity
* @ Retrun DmDensity Getter
**/
Public static float getDmDensityDpi (){
Return dmDensityDpi;
}
/**
* Density factor of the current screen
*
* @ Param DmDensity
* @ Retrun DmDensity Setter
**/
Public static void setDmDensityDpi (float dmDensityDpi ){
DensityUtil. dmDensityDpi = dmDensityDpi;
}
/**
* Density conversion Pixel
**/
Public static int dip2px (float dipValue ){
Return (int) (dipValue * scale + 0.5f );
}
/**
* Pixel conversion Density
**/
Public int px2dip (float pxValue ){
Return (int) (pxValue/scale + 0.5f );
}
@ Override
Public String toString (){
Return "dmDensityDpi:" + dmDensityDpi;
}
}
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.