During page layout, you often need to specify the control width, height, distance from other controls and boundaries, font size, and other units of measurement, what are their meanings and how to switch between them? (Resources are collected from the Internet)
1. dip: Device Independent pixels (device independent pixel ). different devices have different display effects. This is related to the hardware of the device. We recommend that you use this dip to support WVGA, hvga, and qvga without pixels. Equivalent to DP.
Note that the dip is related to the screen density, and the screen density is related to the specific hardware. Incorrect hardware settings may cause the dip to fail to be properly displayed. On a display with a screen density of 160, 1dip = 1px, sometimes your screen resolution may be large, such as 480*800, but the screen density is not properly set, for example, 160, when dip is used, an exception is displayed, which is usually too small.
Dip conversion:
Dip (value) = (INT) (PX (value)/1.5 + 0.5)
In fact, we can simply use dip = px * 2/3.
2. Px: pixels (pixels). Different devices have the same display effect on different display screens. This is an absolute pixel, and the value will never change.
3. SP: scaled pixels (zoom in pixels). It is mainly used to display the best for textsize in the font.
4. Pt (points) -- LB: 1pt = 1/72 inch
Dip should be used as much as possible during layout, and PX should be used less, because different devices have different resolutions and affect visual effects. We recommend that you use dip in pixels and SP in fonts. In the same device, the resolution settings are different. If PX is used as the unit of measurement, different effects are displayed, but dip is not used. You can use DP or SP to specify attributes such as length and height. If you set the font, you need to use sp. DP is not related to density. SP is not only related to density, but also to scale. If the screen density is 160, DP and SP are the same as PX. 1dp = 1sp = 1px, but if PX is used as the unit, if the screen size remains unchanged (assuming it is still 3.2), the screen density is changed to 320. The original textview width is set to 320 PX, And the 3.2-inch screen with a density of 160 is half shorter than the 3.2-inch screen with a density. But if it is set to 160dp or 160sp. The system automatically sets the width property value to 320px. That is, 160*320/160. Among them, 320/160 can be called the density proportion factor. That is to say, if DP and SP are used, the system will automatically convert according to the screen density change.
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.
Resources without any tags are shared in various resolutions.
The switch between PX and dip is as follows:
pixs =dips * (densityDpi/160). dips=(pixs*160)/densityDpi
The switchover method is 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); }