1. 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.
Here to pay special attention to the dip and screen density, and screen density and specific hardware, hardware settings are incorrect, may cause dip can not be normal display. In the screen density of 160 of the display, 1dip=1px, sometimes your screen resolution is very large, such as 480*800, but the screen density is not set correctly, such as or 160, then all the use of DIP will show the exception, the basic is shown too small.
the conversion of dip:
Dip (value) = (int) (PX (value)/1.5 + 0.5)
2. DP: Very simple, and dip is the same.
3. Px:pixels (pixel), Different devices different display effects are the same, this is absolute pixel, is how much will always be how much will not change.
4. sp:scaled pixels (magnified pixel).Mainly used for font display best for textsize.
Note: According to Google's recommendation, Pixel Unified use dip, Font unified use SP
For An example, differentiate between PX and dip:
PX is pixel, if using PX, will use the actual pixel painting, than a bar, drawing a length of 240px horizontal line, in the 480-wide simulator is half the screen width, and in the 320-wide simulator to see is 2/3 screen wide.
And dip, is the screen of the high divided into 480 points, wide divided into 320 points. For example, you make a 160dip horizontal line, whether you are in 320 or 480 of the simulator, is half the length of the screen.
Copy Code code as follows:
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.getresources (). Getdisplaymetrics (). density;
return (int) (Pxvalue/scale + 0.5f);
}