Yesterday, Ali's telephone interview, let me find a lot of shortcomings, a lot of small knowledge points usually do not pay much attention, well, the first blog to analyze the various size units in Android.
Interview Question 1:
"What are the different size units in Android and what are the differences?"
1. Size type
Dip, PX, PT, SP, DP
Dip: (device indepedent pixels) devices independent of pixels (density-independent, can scale with density), different devices display different, it is related to the device, specifically with the number of dots per inch dpi, it is recommended to use dip.
PX: (pixels) pixels, the same size on different devices, so for Android devices have different sizes, the resolution of the device has a large impact on the layout, it is recommended not to use PX.
PT: (points) lbs, it is physical size, 1pt=1/72 inches, the general user of the printing industry.
SP: (scaled pixels) can enlarge pixels, regardless of scale, the size of the general font in Android is recommended to use SP, in Android 1pt is approximately equal to 2.22SP.
DP: Similar to DIP, if the screen density is 160dip (160 dots per inch) 1px=1dp.
2.dip and PX Conversion
We know the dip is independent of screen density pixels, px=dip x screen true density/160.
The DIP has an adaptive screen density that changes the pixel size of the actual control as the screen density. As a result, the display will eventually be converted to PX display.
For example, in Density=160dip and the size of the screen does not change, 1px=1dip.
If a density=160dip, another 320dip and the screen size, if the width of the button 160px, the size of the button with the screen density changes, the size of the button appears at high density is half the original density, unable to complete the adaptive.
If a density=160dip, another 320dip and screen size, if the width of the button to use 160dip, the size of the button does not change with the screen density, with the original actual true size, but high density, the button display more delicate.
3. Density density
The density (density) value represents the number of display points per inch, and the resolution is two concepts. Unlike resolution, the screen size differs due to the same resolution, resulting in different densities.
The total number of pixels on the physical screen. Similar to size, there are also four types: low, medium, high, Ultra High, Android Project project corresponding to drawable-four kinds of dip
WVGA 800*480 Hdip, VGA 640x480 HVGA 480x320 Mdip, Qvag
Note the different resolutions in the RES/DRAWABLE-_DIP directory correspond to different drawable, and the system loads different drawable according to the different resolutions of the system screen.
LDPI: Mobile device with a screen density of 120
MDPI: Mobile device with a screen density of 160 (this is baseline, others are based on this, 1DP = 1px on this device)
HDPI: Mobile device with a screen density of 240
XHDPI: Mobile device with a screen density of 320
XXHDPI: Mobile device with a screen density of 480
4. Resolution
Resolution refers to the total number of pixels that are held on the screen. Note that although the resolution is expressed in most cases as "width x length", the resolution does not imply a screen aspect ratio. In Android, programs generally do not deal directly with resolution.