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.


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


Android已经提供DisplayMetircs类可以很方便的获取分辨率。下面简要介绍DisplayMetics类: Andorid.util包下的DisplayMetrics类提供了一种关于显示...
  • oQiHaoGongYuan
  • oQiHaoGongYuan
  • 2015年09月16日 17:35
  • 1821


  • Android_Tutor
  • Android_Tutor
  • 2009年11月08日 21:50
  • 35120

Android 中的DisplayMetrics类的用法

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


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


1、分辨率和屏幕密度的区别 (1)分辨率 显示分辨率 指屏幕所能显示的像素有多少。 显示分辨率一定的情况下,显示屏越小图像越清晰,反之,显示屏大小固定时,显示分辨率越高图像越清晰。 ...
  • u014058109
  • u014058109
  • 2015年05月22日 21:56
  • 728

Android Dpi dip px 以及DisplayMetrics中density densityDpi的关系

px (pixels)像素 – 是像素,就是屏幕上实际的像素点单位。  dip或dp (device independent pixels)设备独立像素, 与设备屏幕有关。  sp (scaled...
  • Mr_IceFox
  • Mr_IceFox
  • 2014年07月22日 10:25
  • 5506


创建个工具类MyUtils方便使用 public static DisplayMetrics getScreenMetrics(Context context) { Window...
  • daitu_liang
  • daitu_liang
  • 2015年12月05日 18:37
  • 941


一、WindowManager类 1.概述:应用程序使用这个接口与窗口管理器对话,并且每个窗口管理器对象都绑定在一个特定的Display类中。为获取一个WindowManager 窗口管理器并显示,...
  • u012637501
  • u012637501
  • 2014年11月05日 22:25
  • 3009


原文地址: 得到一个屏幕尺寸的三种方法如下: // 通过WindowMana...
  • u010064206
  • u010064206
  • 2015年05月25日 01:31
  • 983
您举报文章:android DisplayMetrics