Android DisplayMetrics 获取和屏幕相关的信息
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 获取和屏幕相关的信息的更多相关文章
- android DisplayMetrics 获取屏幕分辨率
Android 提供DisplayMetircs 类可以很方便的获取分辨率.下面介绍 DisplayMetics 类: Andorid.util 包下的DisplayMetrics 类提供了一种关于显 ...
- Android实现获取应用程序相关信息列表的方法
本文所述为Androdi获取手机应用列表的方法,比如获取到Android应用的软件属性.大小和应用程序路径.应用名称等,获取所有已安装的Android应用列表,包括那些卸载了的,但没有清除数据的应用程 ...
- Android中获取应用程序(包)的信息----PackageManager
本节内容是如何获取Android系统中应用程序的信息,主要包括packagename.label.icon.占用大小等.具体分为两个 部分,计划如下: 第一部分: 获取应用程序的packagena ...
- Android中获取应用程序(包)的信息-----PackageManager的使用(一)
本节内容是如何获取Android系统中应用程序的信息,主要包括packagename.label.icon.占用大小等.具体分为两个 部分,计划如下: 第一部分: 获取应用程序的packagenam ...
- Android中获取应用程序(包)的信息-----PackageManager的使用
本节内容是如何获取Android系统中应用程序的信息,主要包括packagename.label.icon.占用大小等.具体分为两个 部分,计划如下: 第一部分: 获取应用程序的packagename ...
- 【转】Android中获取应用程序(包)的信息-----PackageManager的使用(一)
转载请注明出处:http://blog.csdn.net/qinjuning 本节内容是如何获取Android系统中应用程序的信息,主要包括packagename.label.icon.占 ...
- 获取用户的相关请求信息, 以及包括请求头 request.environ
#在index文件中 1. print(type(request)) #看出所属库 2. from django.core.handlers.wsgi import WSGIRequest #查看WS ...
- Android DisplayMetrics类获取屏幕大小
DisplayMetrics public class DisplayMetrics extends Object java.lang.Object ↳ android.util.Disp ...
- ios 获取设备相关的信息
.获取设备的信息 UIDevice *device = [[UIDevice alloc] int]; NSString *name = device.name; //获取设备所有者的名称 NSStr ...
随机推荐
- 【RT-Thread】线程的基本知识
什么是线程? 人们在生活中处理复杂问题时,惯用的方法就是分而治之,即把一个大问题分解成多个相对简单.比较容易解决的小问题,小问题逐个被解决了,大问题也就随之解决了.同样,在设计一个较为复杂的应用程序时 ...
- 使用jmeter进行压力测试入门讲解
1.下载安装jmeter 略 我这里放上5.1版本的,有需要可以下载 链接:https://pan.baidu.com/s/1xRZZmTY4do1oDU_xPit94Q&shfl=share ...
- 一次对php大马的后门的简单分析
有人分享了一个php大马(说是过waf),八成有后门,简单分析了一次 <?php $password='Shiqi';//登录密码(支持菜刀) //----------功能程序--------- ...
- Ubuntu 终端中文回显乱码
参考文章 : http://wiki.ubuntu.org.cn/%E4%BF%AE%E6%94%B9locale 所用 Ubuntu的版本 : 猜想是这样的: 1.字符的编码和显示时,所处的环境不是 ...
- 微信小程序canvas生成并保存图片
---恢复内容开始--- 微信小程序canvas生成并保存图片,具体实现效果如下图 实现效果需要做以下几步工作 一.先获取用户屏幕大小,然后才能根据屏幕大小来定义canvas的大小 二.获取图 ...
- Did You AK Today? (今天你AK了吗?)
考虑到本文读者年龄原因,本文改为使用简体中文撰写. 题目描述 今有正整数 n,kn,kn,k,求 1−n1-n1−n 共 nnn 个数的全排列,按字典序的第 kkk 个. 数据满足 1≤n≤105,1 ...
- [USACO17JAN]Building a Tall Barn建谷仓
题目描述 Farmer John is building a brand new, NNN -story barn, with the help of his KKK cows ( 1≤N≤K≤101 ...
- alinode与node性能测试方法与分析
需求和技术指标整理 node服务在引入node性能监控过程中,需要使用alinode,为了对alinode与官方node各项性能指标的差异有进一步的认识,现开展以下调研.测试. 原理性分析 alino ...
- 百万年薪python之路 -- 函数的动态参数
1.函数的动态参数 1.1 动态接收位置参数 在参数位置用*表示接受任意参数 def eat(*args): print('我想吃',args) eat('蒸羊羔','蒸熊掌','蒸鹿尾儿','烧花鸭 ...
- Python 常见异常类型
python标准异常 异常名称 描述 BaseException 所有异常的基类Sy ...