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.

最新文章

  1. 【转】[fix] Wireshark error: There are no interfaces on which a capture can be done. on Mac OS X
  2. RabbitMQ Topic exchange
  3. asp.net 琐记
  4. 深入浅出 - Android系统移植与平台开发(七)- 初识HAL
  5. jxl导出Excel文件
  6. Python Web 应用:WSGI基础
  7. ubuntu重置root密码
  8. hdu 2413(最大匹配+二分)
  9. Mysql 笔记:
  10. 雪碧图(sprite)
  11. iOS开发经常使用宏定义
  12. Struts2.1.8 + Spring3.0+ Hibernate3.2整合笔记
  13. HDU 3691 Nubulsa Expo
  14. struts2快速入门
  15. Django(博客系统):按照时间分层筛选“/blog/article/?create_time__year=2017”,出现问题:Database returned an invalid datetime value. Are time zone definitions for your database installed?
  16. 网络基础tcp/ip协议二
  17. 小程序textarea完美填坑
  18. 目前在segmentfault写博客
  19. Mysql Group by 使用解析
  20. Open-Drain与Push-Pull【转】

热门文章

  1. Android-Activity使用(2)
  2. ios-自定义alertView提示框
  3. wkwebview a target="_blank" 打不开链接的解决方案
  4. 注解的基本盘点 -- 《Java编程思想》
  5. 原生js与css3结合的电风扇
  6. ligerui_ligerTree_007_ligerTree动态加载节点
  7. Java中内存中的Heap、Stack与程序运行的关系
  8. 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数001·3D函数
  9. 运行TLD
  10. CSS:opacity 的取值范围是 0~1