# android DisplayMetrics

DisplayMetrics outMetrics =context.getResources().getDisplayMetrics();

getDisplayMetrics

DisplayMetrics getDisplayMetrics ()
Return the current display metrics that are in effect for this resource object. The returned object should be treated as read-only.

Returns
DisplayMetrics  The resource's current display metrics.


DisplayMetrics dm = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);

getDefaultDisplay

Display getDefaultDisplay ()
Returns the Display upon which this WindowManager instance will create new windows.

Despite the name of this method, the display that is returned is not necessarily the primary display of the system (see DEFAULT_DISPLAY). The returned display could instead be a secondary display that this window manager instance is managing. Think of it as the display that this WindowManager instance uses by default.

To create windows on a different display, you need to obtain a WindowManager for that Display. (See the WindowManager class documentation for more information.)

    /**
* The absolute width of the display in pixels.
*/
public int widthPixels;
/**
* The absolute height of the display in pixels.
*/
public int heightPixels;

DisplayMetrics类里面的其他public成员有：

    /**
* 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.
*
* <p>This value does not exactly follow the real screen size (as given by
* {@link #xdpi} and {@link #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).
* @see #DENSITY_DEFAULT
*/
public float density;
/**
* The screen density expressed as dots-per-inch.  May be either
*/
public int densityDpi;
/**
* A scaling factor for fonts displayed on the display.  This is the same
* as {@link #density}, except that it may be adjusted in smaller
* increments at runtime based on a user preference for the font size.
*/
public float scaledDensity;
/**
* The exact physical pixels per inch of the screen in the X dimension.
*/
public float xdpi;
/**
* The exact physical pixels per inch of the screen in the Y dimension.
*/
public float ydpi;

dp 和 px 的转换：
DisplayMetrics dm；
…(dm初始化)
pxValue ＝ dpValue * dm.density + 0.5f (0.5f主要是用来四舍五入)

（链接为： https://developer.android.com/guide/practices/screens_support.html#overview

Converting dp units to pixel units
In some cases, you will need to express dimensions in dp and then convert them to pixels. Imagine an application in which a scroll or fling gesture is recognized after the user’s finger has moved by at least 16 pixels. On a baseline screen, a user’s must move by 16 pixels / 160 dpi, which equals 1/10th of an inch (or 2.5 mm) before the gesture is recognized. On a device with a high-density display (240dpi), the user’s must move by 16 pixels / 240 dpi, which equals 1/15th of an inch (or 1.7 mm). The distance is much shorter and the application thus appears more sensitive to the user.
To fix this issue, the gesture threshold must be expressed in code in dp and then converted to actual pixels. For example:

// The gesture threshold expressed in dp
private static final float GESTURE_THRESHOLD_DP = 16.0f;
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
mGestureThreshold = (int) (GESTURE_THRESHOLD_DP * scale + 0.5f);

// Use mGestureThreshold as a distance in pixels… The
DisplayMetrics.density field specifies the scale factor you must use
to convert dp units to pixels, according to the current screen
density. On a medium-density screen, DisplayMetrics.density equals
1.0; on a high-density screen it equals 1.5; on an extra-high-density screen, it equals 2.0; and on a low-density screen, it equals 0.75.
This figure is the factor by which you should multiply the dp units on
order to get the actual pixel count for the current screen. (Then add
0.5f to round the figure up to the nearest whole number, when converting to an integer.) For more information, refer to the
DisplayMetrics class. However, instead of defining an arbitrary
threshold for this kind of event, you should use pre-scaled
configuration values that are available from ViewConfiguration. Using
pre-scaled configuration values You can use the ViewConfiguration
class to access common distances, speeds, and times used by the
Android system. For instance, the distance in pixels used by the
framework as the scroll threshold can be obtained with
getScaledTouchSlop(): private static final int GESTURE_THRESHOLD_DP =
ViewConfiguration.get(myContext).getScaledTouchSlop(); Methods in
ViewConfiguration starting with the getScaled prefix are guaranteed to
return a value in pixels that will display properly regardless of the
current screen density.

#### Android屏幕适配及DisplayMetrics解析

2015-09-16 20:35:27

#### Android——DisplayMetrics之我见

2012-11-14 14:46:46

#### Android中Display及DisplayMetrics理解

2015-09-16 17:35:20

#### Android获取屏幕分辨率及DisplayMetrics简介

2010-09-19 16:20:00

#### Android突破二之WindowManager、Display、DisplayMetrics类

2014-11-05 22:25:37

#### Android 中的DisplayMetrics类的用法

2012-12-06 15:43:48

#### 屏幕适配以及DisplayMetrics解析

2018-05-22 12:54:17

#### Android基础教程(四)之-----取得手机屏幕大小DisplayMetrics的应用

2009-11-08 21:50:00

#### 初体验AutoLayout(一)

2016-07-29 15:44:11

#### DisplayMetrics

2015-04-08 16:35:10