1, overview
In the past, programmers typically designed computer user interfaces in pixels. For example, the image size is 80x32 pixels. The problem with this approach is that ifyou run the program on a new monitor with a higher dots per inch (dpi), the user interface will appear small. In some cases, the user interface may be too small to see the content. This problem can be solved by developing the program in a resolution-independent unit of measurement. Android app development supports different units of measurement.
2. Meaning of unit of measure
Dip:device independent pixels ( device independent pixels ). Different devices have different display effects , This is related to device hardware, generally we support WVGA , HVGA and the QVGA It is recommended to use this, independent of pixels.
Dp:dip is the same.
Px:pixels ( pixels ). different devices show the same effect, generally we HVGA Representative 320x480 pixels, this is used more.
Pt:point, is a standard length unit,1pt=1/72 inch, for the printing industry, very simple to use;
sp:scaled pixels ( enlarge pixels ). mainly used for font display Best for textsize .
In(inches): unit of length.
MM(mm): unit of length.
3. Conversion formula of unit of measure
In the Android source package Typedvalue.java , we look at the following function:
public static float applydimension (int unit, float value,
Displaymetrics metrics)
{
Switch (unit) {
Case COMPLEX_UNIT_PX:
return value;
Case COMPLEX_UNIT_DIP:
return value * metrics.density;
Case COMPLEX_UNIT_SP:
return value * metrics.scaleddensity;
Case COMPLEX_UNIT_PT:
return value * metrics.xdpi * (1.0F/72);
Case COMPLEX_UNIT_IN:
return value * METRICS.XDPI;
Case COMPLEX_UNIT_MM:
return value * metrics.xdpi * (1.0f/25.4f);
}
return 0;
}
The function: convert units to pixels.
Metrics.density: The default value is density_device/(float) density_default;
Metrics.scaleddensity: The default value is density_device/(float) density_default;
METRICS.XDPI: The default value is density_device;
Density_device: For screen density
Density_default: Default value is
4. Screen density
Represents the number of display points per inch, with a resolution of two different concepts
Android mainly has the following screens: The following table
Screen Tyep |
Width Pixels |
Height Pixels |
Size Range (inches) |
Screen density |
QVGA |
240 |
320 |
2.6-3.0 |
Low |
WQVGA |
240 |
400 |
3.2-3.5 |
Low |
Fwqvga |
240 |
432 |
3.5-3.8 |
Low |
HVGA |
320 |
480 |
3.0-3.5 |
Medium |
WVGA |
480 |
800 |
3.3-4.0 |
High |
FWVGA |
480 |
854 |
3.5-4.0 |
High |
WVGA |
480 |
800 |
4.8-5.5 |
Medium |
FWVGA |
480 |
854 |
5.0-5.8 |
Medium |
Note |
Currently Android default low=120 ;Medium =160; High = |
5. In summary
According to px = dip * density / 160 160 px = dip
according to GOOGLE  proposal, TEXTVIEW  The best use of the font SP  do units, and view textview android default use sp dip
The pixel difference between PX, dip, DP, SP, PT and the screen density of the units in Android determines the appearance of the display effect.