Sp and dp in Android

Source: Internet
Author: User

Dp (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 of TextView font size, and to view the TextView source code, Android uses sp as the font size unit by default.

In addition, the unit used in google Code is px by default.

In the apk resource package, when the screen density is 240, the resources using the hdpi label

Resources that use the mdpi label when the screen density is 160

Ldpi tag resources are used when the screen density is 120.

1dp = 1px on a display at 160 o'clock per inch.

The following describes the mutual conversion of several different units.

Public static int dip2px (Context context, float dipValue ){
Final float scale = context. getResources (). getDisplayMetrics (). density;
Return (int) (dipValue * scale + 0.5f );
}
Public static int px2dip (Context context, float pxValue ){
Final float scale = context. getResource (). getDisplayMetrics (). density;
Return (int) (pxValue/scale + 0.5f );
}
Public static int dip2px (Context context, float dipValue ){
Final float scale = context. getResources (). getDisplayMetrics (). density;
Return (int) (dipValue * scale + 0.5f );
}
Public static int px2dip (Context context, float pxValue ){
Final float scale = context. getResource (). getDisplayMetrics (). density;
Return (int) (pxValue/scale + 0.5f );
}

The following describes how to obtain the Resolution:

Write the following code in the onCreate method of an Activity:
DisplayMetrics metric = new DisplayMetrics ();
GetWindowManager (). getDefaultDisplay (). getMetrics (metric );
Int width = metric. widthPixels; // screen width (pixels)
Int height = metric. heightPixels; // screen height (pixels)
Float density = metric. density; // screen density (0.75/1.0/1.5)
Int densityDpi = metric. densityDpi; // screen density DPI (120/160/240)

 

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 concept in the design, the developer can also take an approximate value through 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.

Reprinted: http://www.cnblogs.com/chiao/archive/2011/07/07/2100216.html

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.