Android DisplayMetrics类获取屏幕大小
DisplayMetrics
public class DisplayMetrics
extends Object
java.lang.Object
↳ android.util.DisplayMetrics
是Android提供的记述屏幕的有关信息的一种结构,诸如其尺寸,密度和字体缩放的一般信息。
第一种方法:
WindowManager wm = (WindowManager) context.getSystemService(
Context.WINDOW_SERVICE);
DisplayMetrics metrics= new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(metric);
第二种方法
DisplayMetrics metrics= new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);)
第三种方法
DisplayMetric metrics=mContext.getResources().getDisplayMetric();
//这俩个就是手机屏幕的屏幕分辨率,物理宽高值如1080*1920(ToolBar或ActionBar会占据一定空间,得到的heightPiexls会小一点)
int width = metrics.widthPixels; // 表示屏幕的像素宽度,单位是px(像素)
int height = metrics.heightPixels; // 表示屏幕的像素高度,单位是px(像素)
float density = metrics.density; // 显示器的逻辑密度,Density Independent Pixel(如3.0)
( metrics.scaledDensity和metrics.density数值是一样的)
int densityDpi = metrics.densityDpi; // 整个屏幕的像素密度DPI(dots per inch每英寸像素数),可以是密度低,密度中等,或者密度高(如240/ 360 / 480)
float xdpi= metrics.xdpi /表示在X轴方向上每英寸的像素值,X轴方向上的DPI(dots per inch)
float ydpi= metrics.ydpi //表示在Y轴方向上每英寸的像素值, Y方向上的DPI
//wm.getDefaultDisplay().getHeight();获得的数据和int height = metrics.heightPixels一样,不过getHeight()方法弃用了,建议使用后者
density详细说明
float density
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.
This value does not exactly follow the real screen size (as given by xdpi and 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).
不需要在AndroidManifest.xml文件额外声明 support-screen节点,因为屏幕适配的几种属性值都默认为true。参考官方文档API.
Android DisplayMetrics类获取屏幕大小的更多相关文章
- wift - 使用UIScreen类获取屏幕大小尺寸
UISreen类代表了屏幕,开发中一般用来获取屏幕相关的属性,例如获取屏幕的大小. 1 2 3 4 5 6 7 //获取屏幕大小 var screenBounds:CGRect = UIScreen. ...
- android 任意类 获得屏幕大小
方法一 WindowManager managet=(WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display ...
- Android获取屏幕大小和设置无标题栏
android获取屏幕大小非常常用,例如写个程序,如果要做成通用性很强的程序,适用屏幕很强,一般布局的时候都是根据屏幕的长宽来定义的,所以我把这个总结一下,方便日后忘记的时候查阅.还有就是有时候写程序 ...
- Android 获取屏幕大小和密度
Android 获取屏幕大小和密度 DisplayMetrics metric = new DisplayMetrics(); getWindowManager().getDefaultDisplay ...
- js获取屏幕大小
1.js获取屏幕大小 <html> <script> function a(){ document.write( "屏幕分辨率为:"+screen.widt ...
- 转:VC++获取屏幕大小第一篇 像素大小GetSystemMetrics
VC++获取屏幕大小第一篇 像素大小 GetSystemMetrics>和<VC++获取屏幕大小第二篇物理大小GetDeviceCaps 上>和<VC++获取屏幕大小第三篇物理 ...
- cocos2d-x JS 获取屏幕大小或中点
以一张背景图为例: var HelloWorldLayer = cc.Layer.extend({ ctor:function () { this._super(); var bg = new cc. ...
- 绘制bitmap 全屏 安卓获取 屏幕大小
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 绘制bitmap 全屏 Rectf rectF = new RectF(0, 0, w, ...
- C#获取屏幕大小或任务栏大小
C#获取屏幕大小或任务栏大小http://www.cnblogs.com/chlyzone/archive/2012/11/05/2754601.html
随机推荐
- Python开发【第六章】:Python面向对象
编程范式 编程是程序员用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程,一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,正所谓条条大路通罗马,实现一个任务的方式有很多种 ...
- 制作图表二、使用图片工厂设置RGB改变图标颜色
亮绿 RGB:76 175 80灰色 RGB:151 151 153
- 打开VS调试不进入开发的网站直接跳转到主页
重启了熟悉有卸载IE11的,搞了好几个小时 最后把电脑管家里的锁定主页打开就好了! 很久之后 我再锁上 也没有这问题了
- iOS,视图控制器相关(UIViewController)
1.视图控制器各个方法调用时机 2.选项卡(Tab Bar)和导航栏(Navigation Bar) 3.有无控制器的页面跳转 4.页面跳转隐藏底部选项卡 5.获取导航栏和状态栏高度,隐藏导航栏返回按 ...
- MVC+EF 自定义唯一性验证
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 本篇提供二种方法,希望大家喜欢 1.自定义验证属性 ...
- Oracle EBS R12 电子技术参考手册 - eTRM (电子文档)
http://etrm.oracle.com/pls/etrm/etrm_search.search
- Oracle配置本地网络服务名
Oracle安装完成后,可以使用客户端自带的的网络配置向导(Net Configuration Assistant)进行配置 1.启动Net Configuration Assistant.选择&qu ...
- Debian系列Linux/Ubuntu 安装软件
wps(http://community.wps.cn/download/) 优客天气(https://launchpad.net/indicator-china-weather/+download) ...
- SQL系统视图表
SQL系统视图表 )) + '学年' + ss.Name AS listtext, sc.SchoolId FROM dbo.SchoolCalendar AS sc INNER JOIN dbo.S ...
- jqueryui引用出错(base is not a constructor,widget no found)
出错的原因,主要是引用顺序出错. 正确顺序如下: query本身必须放在第一位: <script src="../../../Scripts/Jquery1.7.2/jquery-1. ...