android DisplayMetrics

两种方式都是通过获得一个 DisplayMetrics对象来获取屏幕的分辨率。获取DisplayMetrics的方

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



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

DisplayMetrics  The resource's current display metrics.


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



Added in API level 1
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.)

虽然平常的情况,两种方法得到的dm对象里面的值是相同的,但是两者的意义应该是不同的。目前我还没有搞太懂两者的区别,尤其是哪些情况下两者的值不同。网上看到有的小伙伴说推荐使用第一种,即通过Resources来获取 DisplayMetrics对象,但不知其中原因。其实这肯定可以通过源代码调试来找到,暂时先放着,有机会弄清楚区别。

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


     * 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
     * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}.
    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;
pxValue = dpValue * dm.density + 0.5f (0.5f主要是用来四舍五入)


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.

下一篇第6章 Java访问控制权限
想对作者说点什么? 我来说一句