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
随机推荐
- SVD奇异值分解
奇异值分解 备忘:Eigen类库可能会和其他库产生冲突,将Eigen类库的头文件引用放到前面解决了.
- GitLab + Jenkins + Docker + Kubernetes。
目前方案是GitLab + Jenkins + Docker + Kubernetes. 方案的工作流程如下:首先,开发人员提交代码代码提交:随后,GitLab 会自动触发Jenkins job,Je ...
- sql 里 text类型的操作(转载)
SQL Server中TEXT类型字段值在数据库中追加字符串方法 对text类型字段值进行追加更新的操作,一开始用了简单的update语句试了试,有错误,原来text.ntext类型的字段不能和 va ...
- EBS Technology Codelevel Checker (ETCC)
ETCC提供了两个脚本,用来帮助用户检测 E-Business Suite Release 12.2数据库和中间层是否安装了必须的补丁.官方强烈推荐使用. 1.checkDBpatch.sh 这个脚本 ...
- Oracle配置本地网络服务名
Oracle安装完成后,可以使用客户端自带的的网络配置向导(Net Configuration Assistant)进行配置 1.启动Net Configuration Assistant.选择&qu ...
- SQL注入POC
mysql #encoding=utf-8 import httplib import time import string import sys import random import urlli ...
- Eclipse中使用自己封装的jar包的过程
在包名上右键,选择Export"" 经过上面的步骤,成功导出了可运行jar包,下面在另一个自己的工程里引入这个jar包 最终效果如下:可见包已经可以正常使用了!! 运行自己的jar ...
- 处理 httprequest post 编码问题
site :http://www.cnblogs.com/artwl/archive/2012/03/07/2382848.html 在JavaScript中推荐的做法是用encodeURI对URI的 ...
- jQuery与其他JS库冲突解决
实际开发中遇到JQuery与其他js库起冲突 究其原因,是它们的全局对象定义冲突了,特别是变量”$”, 可重载$函数.使用jQuery.noConflict()就可以通过重载$函数 例:项目中应用的 ...
- requirejs中 shim
使用requireJS的shim参数,完成jquery插件的加载 时间 2014-10-31 13:59:10 CSDN博客 原文 http://blog.csdn.net/aitangyong/ ...