Android源码中是这样来描述DisplayMetrics的。

/**
* A structure describing general information about a display, such as its
* size, density, and font scaling.
* <p>To access the DisplayMetrics members, initialize an object like this:</p>
* <pre> DisplayMetrics metrics = new DisplayMetrics();
* getWindowManager().getDefaultDisplay().getMetrics(metrics);</pre>
*/

按照DisplayMetrics注释中的那样,我们直接写个例子来测试下,就什么都明白了。我用的是小米3:

        DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics); /**
* 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).
*
* density 是逻辑上的屏幕密度,约定在160dpi的设备上,1px=1dip。
* density = densityDpi / 160
*
* @see #DENSITY_DEFAULT
*/
float density = metrics.density;
/**
* The screen density expressed as dots-per-inch. May be either
* {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}.
* densityDpi 表示每英寸的点数(并不是像素数)
*/
int densityDpi = metrics.densityDpi;
/**
* The absolute height of the display in pixels.
* 屏幕的绝对高度,以像素为单位。
*/
int heightPixels = metrics.heightPixels;
/**
* The absolute width of the display in pixels.
* 同样,这个是屏幕的绝对宽度,以像素为单位。
*/
int widthPixels = metrics.widthPixels; /**
* The exact physical pixels per inch of the screen in the X dimension.
* 横向每一英寸确切的物理像素,我们可以尝试通过这个值和widthPixels 来计算屏幕的宽度英寸。
*/
float xdpi = metrics.xdpi;
/**
* The exact physical pixels per inch of the screen in the Y dimension.
* 横向每一英寸确切的物理像素,我们可以尝试通过这个值和heightPixels 来计算屏幕的高度英寸。
*/
float ydpi = metrics.ydpi;
/**
* 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.
* scaledDensity 这个值从上面的注释看,是和字体相关的factor,通常情况下这个值和density是相等的,但是假如用户手动的调整了
* 系统字体的大小,那么这个值就有可能改变(以小米3为例,标准字体,这个值=3,最大字体这个值=3.25)。
* 因此现在很多情况下,我们把字体单位写成dp,尽管Google建议的字体让写成sp。
*/
float scaledDensity = metrics.scaledDensity;
LogUtil.logd(TAG, "metrics.density = " + density);
LogUtil.logd(TAG, "metrics.densityDpi = " + densityDpi);
LogUtil.logd(TAG, "metrics.heightPixels = " +heightPixels);
LogUtil.logd(TAG, "metrics.widthPixels = " +widthPixels);
LogUtil.logd(TAG, "metrics.xdpi = " +xdpi);
LogUtil.logd(TAG, "metrics.ydpi = " +ydpi);
LogUtil.logd(TAG, "metrics.scaledDensity = " +scaledDensity); //来计算手机是几英寸的
float pixelsToDipWidth = widthPixels / xdpi;
float pixelsToDipHeight = heightPixels / ydpi;
LogUtil.logd(TAG, "pixelsToDipWidth = " + pixelsToDipWidth);
LogUtil.logd(TAG, "pixelsToDipHeight = " + pixelsToDipHeight);
double mobileInch = Math.sqrt(pixelsToDipHeight * pixelsToDipHeight + pixelsToDipWidth * pixelsToDipWidth);
//我用的小米3,得到的值是4.917646062686045
LogUtil.logd(TAG, "mobileInch = " + mobileInch);

通过这个例子,我们明白了dip及px代表着什么,我们就可以来写出dip与px相互转换的方法。(dip = px / density)

    /**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
} /**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}

上面 加的这个0.5f的原因,Google官方文档中其实有介绍:

Then add 0.5f to round the figure up to the nearest whole number, when converting to an integer.

说白了就是为了四舍五入。。。

比如我上面的例子:

mobileInch = 4.917646062686045 ,我+0.5f(=5.417646062686045),然后再取整之后,就得到5。

Android DisplayMetrics 获取和屏幕相关的信息的更多相关文章

  1. android DisplayMetrics 获取屏幕分辨率

    Android 提供DisplayMetircs 类可以很方便的获取分辨率.下面介绍 DisplayMetics 类: Andorid.util 包下的DisplayMetrics 类提供了一种关于显 ...

  2. Android实现获取应用程序相关信息列表的方法

    本文所述为Androdi获取手机应用列表的方法,比如获取到Android应用的软件属性.大小和应用程序路径.应用名称等,获取所有已安装的Android应用列表,包括那些卸载了的,但没有清除数据的应用程 ...

  3. Android中获取应用程序(包)的信息----PackageManager

    本节内容是如何获取Android系统中应用程序的信息,主要包括packagename.label.icon.占用大小等.具体分为两个 部分,计划如下:   第一部分: 获取应用程序的packagena ...

  4. Android中获取应用程序(包)的信息-----PackageManager的使用(一)

    本节内容是如何获取Android系统中应用程序的信息,主要包括packagename.label.icon.占用大小等.具体分为两个 部分,计划如下:  第一部分: 获取应用程序的packagenam ...

  5. Android中获取应用程序(包)的信息-----PackageManager的使用

    本节内容是如何获取Android系统中应用程序的信息,主要包括packagename.label.icon.占用大小等.具体分为两个 部分,计划如下: 第一部分: 获取应用程序的packagename ...

  6. 【转】Android中获取应用程序(包)的信息-----PackageManager的使用(一)

    转载请注明出处:http://blog.csdn.net/qinjuning       本节内容是如何获取Android系统中应用程序的信息,主要包括packagename.label.icon.占 ...

  7. 获取用户的相关请求信息, 以及包括请求头 request.environ

    #在index文件中 1. print(type(request)) #看出所属库 2. from django.core.handlers.wsgi import WSGIRequest #查看WS ...

  8. Android DisplayMetrics类获取屏幕大小

    DisplayMetrics public class DisplayMetrics   extends Object java.lang.Object     ↳ android.util.Disp ...

  9. ios 获取设备相关的信息

    .获取设备的信息 UIDevice *device = [[UIDevice alloc] int]; NSString *name = device.name; //获取设备所有者的名称 NSStr ...

随机推荐

  1. Python 爬虫(四):Selenium 框架

    Selenium 是一个用于测试 Web 应用程序的框架,该框架测试直接在浏览器中运行,就像真实用户操作一样.它支持多种平台:Windows.Linux.Mac,支持多种语言:Python.Perl. ...

  2. 超详细的FreeRTOS移植全教程——基于srm32

    ### 准备 在移植之前,我们首先要获取到FreeRTOS的官方的源码包.这里我们提供两个下载链接: > 一个是官网:http://www.freertos.org/ > 另外一个是代码托 ...

  3. SDI接口基于FPGA GTP实现

    SDI采集和显示,基于xilinx 7系列器件进行实现,注意事项有如下几点: 1,如果多路SDI共用一个GTP Quad,或是SDI和PCIE在一个GTP Quad,时钟资源应该进行共享,既GTP c ...

  4. 【TencentOS tiny】 超详细的TencentOS tiny移植到STM32F103全教程

    移植前的准备工作 1. 获取STM32的裸机工程模板 STM32的裸机工程模板直接使用野火STM32开发板配套的固件库例程即可.可以从我github上获取https://github.com/jiej ...

  5. Spring Boot 定时任务 @Scheduled

    项目开发中经常需要执行一些定时任务,比如在每天凌晨,需要从 implala 数据库拉取产品功能活跃数据,分析处理后存入到 MySQL 数据库中.类似这样的需求还有许多,那么怎么去实现定时任务呢,有以下 ...

  6. BF算法(蛮力匹配)

    输入主串a,模式b b在a中的位置 1.在串a和串b中设置比较的下标i=0,j=0: 2.重复下述操作,直到a或b的所有字符均比较完毕: 2.1如果a[i]等于b[i],继续比较a和b的下一对字符: ...

  7. 想转行做程序员,是学习JAVA还是Python?哪个更好?

    请大家务必审题,转行做程序员,是程序员,并非数据分析也不是软件测试. 首先声明:这是一篇容易引起撕逼的问答,为了祖国和谐,人民安康,请各位看官尽量理性讨论. 同时,这篇文章是面向一些初入行的朋友进行一 ...

  8. web前端Vue+Django rest framework 框架 生鲜电商项目实战视频教程 ☝☝☝

    web前端Vue+Django rest framework 框架 生鲜电商项目实战视频教程    web前端Vue+Django rest framework 框架 生鲜电商项目实战视频教程 学习 ...

  9. ZGC介绍

    zgc是一款可拓展的低时延,为实现以下几个目标而诞生的垃圾回收器: 停顿时间不超过10ms 停顿时间不会导致堆大小增长 堆大小范围可支持几G到几T 再看一下zgc的标签: region-based ( ...

  10. [书籍翻译] 《JavaScript并发编程》第五章 使用Web Workers

    本文是我翻译<JavaScript Concurrency>书籍的第五章 使用Web Workers,该书主要以Promises.Generator.Web workers等技术来讲解Ja ...