The difference of Android DIP,PX,PT,SP is detailed _android

Source: Internet
Author: User

Dip:device independent pixels (device independent pixel). Different devices have different display effects, this is related to the device hardware, we generally support the WVGA, HVGA and QVGA recommended to use this, do not rely on pixels.

px:pixels (pixel). Different devices display the same effect, generally we hvga represent 320x480 pixels, this use more.
Pt:point, is a standard length unit, 1pt=1/72 inches, used in the printing industry, is very simple and easy to use;
sp:scaled pixels (zoom in pixels). Mainly used for font display best for textsize. Thus, according to Google's advice, TextView's

Font size It is best to use SP to do units, and to view the source of the TextView, Android defaults to use the SP as the font size unit.

In Android, 1pt is probably equal to 2.22SP for reference, and if the UI can provide the best design for the SP, the developer can also get an approximate value by the appropriate conversion if the design does not have an SP concept.

In the past, programmers used to design computer user interfaces in pixel units. For example, define a form field with a width of 300 pixels, the spacing between columns is 5 pixels, the icon size is 16x16 pixels, and so on. The problem with this is that if you run the program on a new monitor that is higher in dots per inch (dpi), the user interface will look small. In some cases, the user interface may be so small that it is difficult to see the content.
Resolution-independent units of measurement can solve this problem. Android supports all of the following units.
PX (pixels): A point on the screen.
In (inches): unit of length.
MM (mm): unit of length.
PT (lb): 1/72 inches.
DP (density-independent pixels): An abstract unit based on screen density. 1DP = 1px on display at 160 dots per inch.
Dip: Same as DP, used in android/ophone example.
SP (Pixel-independent): Similar to a DP, but can be scaled based on the user's font size preference.
Resolution: The whole screen is how many points, such as 800x480, it is for the software to display units, PX as the unit point. The density (density) value represents the number of display points per inch, and the resolution is two concepts. In the APK resource bundle,
Resources using HDPI tags when the screen is density=240
Resources that use MDPI tags when the screen is density=160
Use LDPI label resources when the screen is density=120.

General Android set the length and width of multiple dip, set the size of the font for multiple sp. In screen density of 160,1dp=1px=1dip, 1pt = 160/72 sp 1pt = 1/72 inches. When the screen density is 240, 1dp=1dip=1.5px.
In order for the user interface to be displayed properly on the current and future display types, it is recommended that you always use the SP as the size of the text, and dip as the unit of the other elements. Of course, you can also consider using vector graphics, not bitmaps.

Copy Code code as follows:

Import Android.content.Context;
Import Android.util.DisplayMetrics;
/**
* Calculation 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;
/**
*
* Based on the constructor to obtain the current phone screen factor
*
* */
Public Densityutil {
Get 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 for the current screen
*
* @param dmdensity
* @retrun dmdensity Getter
* */
public static float getdmdensitydpi () {
return dmdensitydpi;
}
/**
* Density factor for 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

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.