1. 单位
- px(pixel): 普通的像素点描述,位图中的一个颜色点(RGBA或者YUV)
- dip(device independent pixels): 设备独立像素。
- dp: 与dip一样。
- pt(point): 标准长度单位。1pt = 1.0 / 72 (in)。
- sp(scaled pixel): 用于字体显示。
- in(inch): 英寸
- mm(millimeter): 毫米
2. 屏幕密度
屏幕类型 | 宽度(pixels) | 高度(pixels) | 尺寸(inches) | 屏幕密度 |
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 |
默认屏幕密度:low = 120; medium = 160; high = 240
3. 屏幕单位的换算
/frameworks/base/core/java/android/util/TypedValue.java
303 /** 304 * Converts an unpacked complex data value holding a dimension to its final floating 305 * point value. The two parameters <var>unit</var> and <var>value</var> 306 * are as in {@link #TYPE_DIMENSION}. 307 * 308 * @param unit The unit to convert from. 309 * @param value The value to apply the unit to. 310 * @param metrics Current display metrics to use in the conversion -- 311 * supplies display density and scaling information. 312 * 313 * @return The complex floating point value multiplied by the appropriate 314 * metrics depending on its unit. 315 */ 316 public static float applyDimension(int unit, float value, 317 DisplayMetrics metrics) 318 { 319 switch (unit) { 320 case COMPLEX_UNIT_PX: 321 return value; 322 case COMPLEX_UNIT_DIP: 323 return value * metrics.density; 324 case COMPLEX_UNIT_SP: 325 return value * metrics.scaledDensity; 326 case COMPLEX_UNIT_PT: 327 return value * metrics.xdpi * (1.0f/72); 328 case COMPLEX_UNIT_IN: 329 return value * metrics.xdpi; 330 case COMPLEX_UNIT_MM: 331 return value * metrics.xdpi * (1.0f/25.4f); 332 } 333 return 0; 334 }
/frameworks/base/core/java/android/util/DisplayMetrics.java
public static final int DENSITY_LOW = 120;
public static final int DENSITY_MEDIUM = 160;
public static final int DENSITY_TV = 213;
public static final int DENSITY_HIGH = 240;
public static final int DENSITY_XHIGH = 320;
public static final int DENSITY_400 = 400;
public static final int DENSITY_XXHIGH = 480;
public static final int DENSITY_560 = 560;
public static final int DENSITY_XXXHIGH = 640;
public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;
public static final float DENSITY_DEFAULT_SCALE = 1.0f / DENSITY_DEFAULT;
public static int DENSITY_DEVICE = getDeviceDensity();
density = DENSITY_DEVICE / (float) DENSITY_DEFAULT;
px = dip * density = dip * DENSITY_DEVICE / DENSITY_DEFAULT.