参考链接: http://developer.android.com/reference/android/util/DisplayMetrics.html

1
DisplayMetrics metrics = getResources().getDisplayMetrics();

public static final int DENSITY_DEFAULT

  • The reference density used throughout the system.
  • Constant Value: 160 (0x000000a0)

public static final int DENSITY_HIGH

  • Standard quantized DPI for high-density screens.
  • Constant Value: 240 (0x000000f0)

public static final int DENSITY_LOW

  • Standard quantized DPI for low-density screens.
  • Constant Value: 120 (0x00000078)

public static final int DENSITY_MEDIUM

  • Standard quantized DPI for medium-density screens.
  • Constant Value: 160 (0x000000a0)

public static final int DENSITY_XHIGH

  • Added in API level 9
  • Standard quantized DPI for extra-high-density screens.
  • Constant Value: 320 (0x00000140)

public static final int DENSITY_XXHIGH

  • Added in API level 16
  • Standard quantized DPI for extra-extra-high-density screens.
  • Constant Value: 480 (0x000001e0)

public static final int DENSITY_XXXHIGH

  • Added in API level 18
  • Standard quantized DPI for extra-extra-extra-high-density screens. Applications should not generally worry about this density; relying on XHIGH graphics being scaled up to it should be sufficient for almost all cases. A typical use of this density would be 4K television screens – 3840x2160, which is 2x a traditional HD 1920x1080 screen which runs at DENSITY_XHIGH.
  • Constant Value: 640 (0x00000280)

public float density

  • The logical density of the display. This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen (for example a 240x320, 1.5”x2” screen), providing the baseline of the system’s display. Thus on a 160dpi screen this density value will be 1; on a 120 dpi screen it would be .75; etc.
  • This value does not exactly follow the real screen size (as given by xdpi and ydpi, but rather is used to scale the size of the overall UI in steps based on gross changes in the display dpi. For example, a 240x320 screen will have a density of 1 even if its width is 1.8”, 1.3”, etc. However, if the screen resolution is increased to 320x480 but the screen size remained 1.5”x2” then the density would be increased (probably to 1.5).

public int densityDpi

  • The screen density expressed as dots-per-inch. May be either DENSITY_LOW, DENSITY_MEDIUM, or DENSITY_HIGH.

public float scaledDensity

  • A scaling factor for fonts displayed on the display. This is the same as density, except that it may be adjusted in smaller increments at runtime based on a user preference for the font size.

public int widthPixels

  • The absolute width of the display in pixels.

public int heightPixels

  • The absolute height of the display in pixels.

public float xdpi

The exact physical pixels per inch of the screen in the X dimension.

public float ydpi

The exact physical pixels per inch of the screen in the Y dimension.

Tips

常量成员有部分只能在高版本 API 中使用。

变量成员除 scaledDensity 跟设置的字体大小有关之外,其他变量直接由设备屏幕决定。

1
2
3
4
5
6
// 三星S7572
// 正常字体
DisplayMetrics{density=1.5, width=480, height=800, scaledDensity=1.5, xdpi=160.42105, ydpi=160.0}

// 超大字体
DisplayMetrics{density=1.5, width=480, height=800, scaledDensity=1.9499999, xdpi=160.42105, ydpi=160.0}