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类获取屏幕大小的更多相关文章

  1. wift - 使用UIScreen类获取屏幕大小尺寸

    UISreen类代表了屏幕,开发中一般用来获取屏幕相关的属性,例如获取屏幕的大小. 1 2 3 4 5 6 7 //获取屏幕大小 var screenBounds:CGRect = UIScreen. ...

  2. android 任意类 获得屏幕大小

    方法一 WindowManager managet=(WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display ...

  3. Android获取屏幕大小和设置无标题栏

    android获取屏幕大小非常常用,例如写个程序,如果要做成通用性很强的程序,适用屏幕很强,一般布局的时候都是根据屏幕的长宽来定义的,所以我把这个总结一下,方便日后忘记的时候查阅.还有就是有时候写程序 ...

  4. Android 获取屏幕大小和密度

    Android 获取屏幕大小和密度 DisplayMetrics metric = new DisplayMetrics(); getWindowManager().getDefaultDisplay ...

  5. js获取屏幕大小

    1.js获取屏幕大小 <html> <script> function a(){ document.write( "屏幕分辨率为:"+screen.widt ...

  6. 转:VC++获取屏幕大小第一篇 像素大小GetSystemMetrics

    VC++获取屏幕大小第一篇 像素大小 GetSystemMetrics>和<VC++获取屏幕大小第二篇物理大小GetDeviceCaps 上>和<VC++获取屏幕大小第三篇物理 ...

  7. cocos2d-x JS 获取屏幕大小或中点

    以一张背景图为例: var HelloWorldLayer = cc.Layer.extend({ ctor:function () { this._super(); var bg = new cc. ...

  8. 绘制bitmap 全屏 安卓获取 屏幕大小

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 绘制bitmap 全屏 Rectf rectF = new RectF(0, 0, w, ...

  9. C#获取屏幕大小或任务栏大小

    C#获取屏幕大小或任务栏大小http://www.cnblogs.com/chlyzone/archive/2012/11/05/2754601.html

随机推荐

  1. mongodb的使用

    1.启动mongodb 启动mongodb在Linux中可以进入mongodb的bin目录下执行      ./mongod -dbpath=所建立的数据文件夹  -logpath=所建立的日志文件 ...

  2. des加密解密的两个方法

    <?php //$input - stuff to decrypt //$key - the secret key to use function do_mencrypt($input, $ke ...

  3. JQuery + XML作为前后台数据交换格式实践

    JQuery + xml作为前后台数据交换 JQuery提供良好的异步加载接口AJAX,可以局部更新页面数据, http://api.jquery.com/category/ajax/ xml作为一种 ...

  4. 《30天自制操作系统》18_day_学习笔记

    harib15a: 到这里为止,我们已经能实现窗口的切换了.我们发现所有的窗口都有光标闪烁,而我们只希望可以接受输入的窗口有光标闪烁.这里我们先来修改任务A中的光标闪烁,当按下TAB时,如果让A不现实 ...

  5. C/C++ 结构体 函数传递

    #include <stdio.h> #include <stdlib.h> struct student{ int num; ]; double dec; }; void s ...

  6. 关于spring-servlet.xml cannot be opened because it does not exist的解决

    右键项目---->properties--->Java Build Path--->source--->Add Folder --->选择conf文件夹

  7. Docker 在6.4上安装

      1 在 CentOS 6.4 上安装 docker docker当前官方只支持Ubuntu,所以在 CentOS 安装Docker比较麻烦(Issue #172). docker官方文档说要求Li ...

  8. Python快速建站系列-Part.Five.2-个人主页及文章列表

    |版权声明:本文为博主原创文章,未经博主允许不得转载. 从usercen.html就可以发现我为个人主页设了三个分开的小版面:写文章.个人文章目录.个人资料 所以按顺序Part.Five的第二部分就完 ...

  9. html,移动端代码

    <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale= ...

  10. Entity FrameWork对有外键关联的数据表的添加操作

    前天做了一个MVC Entity FrameWork项目,遇到有外键关联的数据编辑问题.当你编辑的时候,按照正常的逻辑,把每个字段的数据都对号入座了,然后点击保存按钮,本以为会顺理成章的编辑数据,但是 ...