[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.
======================================
Conversion (using sp and pt as an example)
View the source code of TextView and other classes. We can see that:
Case COMPLEX_UNIT_PX:
Return value;
Case COMPLEX_UNIT_SP:
Return value * metrics. scaledDensity;
Case COMPLEX_UNIT_PT:
Return value * metrics. xdpi * (1.0f/72 );
--------------------------
ScaledDensity = DENSITY_DEVICE/(float) DENSITY_DEFAULT;
Xdpi = DENSITY_DEVICE;
--------------------------
DENSITY_DEFAULT = DENSITY_MEDIUM = 160;
========================================================== ====
Therefore, if pt and sp take the same value of 1, the coefficient between 1pt and 1sp can be set to x,
1 * DENSITY_DEVICE/72 = x * 1 * DENSITY_DEVICE/160 = & gt;
X = 160/72 = 2.2222
That is to say, in Android, 1pt is roughly equal to 2.22sp
========================================================== ======================================
What are Dip and Sp?
In the past, programmers often designed computer user interfaces in pixels. For example, define a 300-pixel table
For a single field, the spacing between columns is 5 pixels, And the Icon size is 16 × 16 pixels. The problem is that, for example
If you run the program on a new monitor with a higher dpi, the user interface will appear very small. In
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.
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.