android DisplayMetrics

原创 2016年05月31日 17:44:58

两种方式都是通过获得一个 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.



Android 屏幕适配方案

转载请标明出处:; 本文出自:【张鸿洋的博客】 1、概述大家在Andr...

Android DisplayMetrics类简介

Android 获取屏幕分辨率及 DisplayMetrics 简介 Android 可设置为随着窗口大小调整缩放比例,但即便如此,手机程序设计人员还是必须知道手机 屏幕的边界,以避免缩放造成的布局变...


一、基本概念 1、屏幕尺寸 屏幕尺寸指屏幕的对角线的长度,单位是英寸(in),1英寸=2.54厘米2、px 是英文单词pixel的缩写,意为像素,屏幕上的点。我们通常所说的分辨率如480X800...




Android之px/dpi/dip/dp/density/DisplayMetrics和屏幕适配文章链接:友情链接:知识点 px、DPI、density和dip/dp的简介; Android系统设置...

Android获取手机屏幕大小 — DisplayMetrics取得画面宽高的方法


Android教程(十二)-- 使用DisplayMetrics获取手机分辨率

在本章教程中,我们将会学习如何使用DisplayMetrics获取手机分辨率。 是这样的,在Android手机如此风扉全球时期,手机的差异性也因为各个生产厂商的战略目标不一样,导致手机的差异性也日渐...


Android 可设置为随着窗口大小调整缩放比例,但即便如此,手机程序设计人员还是必须知道手机屏幕的边界,以避免缩放造成的布局变形问题。 手机的分辨率信息是手机的一项重要信息,很好的是,Andr...