源码:
/frameworks/base/core/java/android/util/DisplayMetrics.java
194 /** 195 * The logical density of the display. This is a scaling factor for the 196 * Density Independent Pixel unit, where one DIP is one pixel on an 197 * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), 198 * providing the baseline of the system's display. Thus on a 160dpi screen 199 * this density value will be 1; on a 120 dpi screen it would be .75; etc. 200 * 201 * <p>This value does not exactly follow the real screen size (as given by 202 * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of 203 * the overall UI in steps based on gross changes in the display dpi. For 204 * example, a 240x320 screen will have a density of 1 even if its width is 205 * 1.8", 1.3", etc. However, if the screen resolution is increased to 206 * 320x480 but the screen size remained 1.5"x2" then the density would be 207 * increased (probably to 1.5). 208 * 209 * @see #DENSITY_DEFAULT 210 */ 211 public float density;默认:
299 public void setToDefaults() { 300 widthPixels = 0; 301 heightPixels = 0; 302 density = DENSITY_DEVICE / (float) DENSITY_DEFAULT; 303 densityDpi = DENSITY_DEVICE; 304 scaledDensity = density; 305 xdpi = DENSITY_DEVICE; 306 ydpi = DENSITY_DEVICE; 307 noncompatWidthPixels = widthPixels; 308 noncompatHeightPixels = heightPixels; 309 noncompatDensity = density; 310 noncompatDensityDpi = densityDpi; 311 noncompatScaledDensity = scaledDensity; 312 noncompatXdpi = xdpi; 313 noncompatYdpi = ydpi; 314 }注释:
默认情况:density = DENSITY_DEVICE / (float) DENSITY_DEFAULT;
DENSITY_DEVICE和DENSITY_DEFAULT在DisplayMetrics.java都有说明:
35 /** 36 * Standard quantized DPI for medium-density screens. 37 */ 38 public static final int DENSITY_MEDIUM = 160; ... 155 public static final int DENSITY_DEFAULT = DENSITY_MEDIUM; ... 162 163 /** 164 * The device's current density. 165 * <p> 166 * This value reflects any changes made to the device density. To obtain 167 * the device's stable density, use {@link #DENSITY_DEVICE_STABLE}. 168 * 169 * @hide This value should not be used. 170 * @deprecated Use {@link #DENSITY_DEVICE_STABLE} to obtain the stable 171 * device density or {@link #densityDpi} to obtain the current 172 * density for a specific display. 173 */ 174 @Deprecated 175 public static int DENSITY_DEVICE = getDeviceDensity(); ... 370 private static int getDeviceDensity() { 371 // qemu.sf.lcd_density can be used to override ro.sf.lcd_density 372 // when running in the emulator, allowing for dynamic configurations. 373 // The reason for this is that ro.sf.lcd_density is write-once and is 374 // set by the init process when it parses build.prop before anything else. 375 return SystemProperties.getInt("qemu.sf.lcd_density", 376 SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT)); 377 }即:dpi为160时,density为1