前言,我写到后面发现这里有个帖子不错;http://www.miui.com/thread-413844-1-1.html

所以你们可以选择不向下看了!

在reference/android/util/DisplayMetrics.html中有如何获得Metrics(即屏幕显示信息,如大小啊密度啊)的代码。

如下 :

 DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics);

dpi:dots per inch,每英寸中的像素数.

xdpi:即水平方向每英寸中的像素数,

ydpi:即垂直方向每英寸的像素数.

关于xdpi,ydpi,android参考中是这样介绍的: xdpi:The exact physical pixels per inch of the screen in the X demension. 即,屏幕在X方向上每英寸中精确的物理像素。 ydpi:The exact physical pixels per inch of the screen in the Y dimension.

要知道是dp是dip是简写,不要把dp,dip和dpi混淆了!

dip,dp;device independent pixel.设置独立像素.

density:这个东西将dip和dpi联系在一起了!

参见android中的文档 :docs/reference/android/util/DisplayMetrics.html

density是显示的逻辑密度,是用于设备独立像素(dip)的扩展因子,在一个160dpi的屏幕里,一个像素几乎就等于一个dip.

(例如,一个240*320,1.5寸宽,2.0寸长 的的屏幕),它提供了系统显示基线.因此在一个160dpi的屏幕上这个密度值是1.

在一个120dpi的屏幕中将是0.75.

即公式出来了:density = dpi/160;

原文:

The logical density of the display.This is a scaling factor for the Density Independent Pixel unit,where one DIP is is one pixel on an approximately 160 dpi screen(for example a 240*320,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 0.75;etc.
这个值并不完全等同于真空屏幕的尺寸(由xdpi和ydpi给定,但是用于在屏幕dpi增大的时候扩展整个UI的尺寸,例如,一个240

x320的屏幕将拥有密度值为1,而不管它的宽度只有1.8寸,1.3寸等等.)然而,如果屏幕的分辨率增加到320x480而这个屏幕的尺寸依然是1.5寸x2寸.那么密度将增加(大概到1.5)(译注:这个还是翻译得不好的,有些地方没有怎么看明白!)

原文:

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)

关于这个android中尺寸的值请参见:

docs/guide/topics/resources/more-resources.html#Dimension

下面是一个asus平板的输出的metrics信息:

1.03-20 11:09:49.240:metrics={xdpi:160.15764,ydpi:160.0,width:800,height:1232,density:1.0}

2.使用默认模拟器WVGA800

03-20 03:15:32.282: I/ClientStation(280): metrics={xdpi:240.0,ydpi:240.0,width:480,height:800,density:1.5}

3.这个还是使用默认的模拟器,但是分辨率被我设置成了:800x1232

03-20 03:35:25.006: I/ClientStation(337): metrics={xdpi:240.0,ydpi:240.0,width:800,height:1232,density:1.5}

4.一个更详细点的数据,默认的模拟器:

03-20 05:58:32.369: I/Demo(584): metrics={xdpi:240.0,ydpi:240.0,width:480,height:800,density:1.5,densityDpi:240,widthPixels: 800}

5. asus

03-20 14:04:23.702: I/Demo(13824): metrics={xdpi:160.15764,ydpi:160.0,width:800,height:1232,density:1.0,densityDpi:160,widthPixels: 1232}


6.通过给一个TextView设置一个300dp看一下在这个屏幕下有倒底占用了多大空间. 03-20 14:29:27.792: I/Demo(14453): metrics={xdpi:160.15764,ydpi:160.0,width:800,height:1232,density:1.0,densityDpi:160,widthPixels: 1232},300dp= 300Pixels,lineHeight = 19 7. 使用默认模拟器上的数据 03-20 06:16:29.079: I/Demo(722): metrics={xdpi:240.0,ydpi:240.0,width:480,height:800,density:1.5,densityDpi:240,widthPixels: 800},300dp= 450Pixels

为让大家不把dip,和dpi混淆了.我下面还是将dip 写成dp算了哈. 可能简单的发现:240/1.5 = 160. 也就是说: density = dpi/160; dp/px = dpi/160 = density 1dp = px*density 也就是说一个屏幕总的宽度dp为 width/(px*density) = width/density
我用的上面那个平板也就是: 800/1 = 800dp. 默认的模拟器也就是480dp


感谢你,还看到了这里!哈哈!



更多相关文章

  1. 【AS基础篇二:线性布局和相对布局的使用】:设计一个简单的电影界
  2. 在Android2.2模拟器上安装Android(安卓)Market
  3. Android调节屏幕亮度分析源码
  4. Android(安卓)自己制作的相册--效果还不错哦
  5. Android用户手势检测
  6. Android中通过按键旋转屏幕
  7. Ubuntu下编译android的linux内核
  8. Android: 模拟器system分区没有可用空间的问题
  9. Android(安卓)中Activity生命周期分析:Android中横竖屏切换时的生

随机推荐

  1. android installd分析
  2. AS中一个报错解决:LoggedErrorException:
  3. 2011.09.01(5)——— android checkbox自定
  4. Android通过http协议POST传输方式
  5. Android中短信拦截解决方案
  6. 新建项目出现android support library问
  7. Android(安卓)WebRTC
  8. Android笔记
  9. 【android】对canvas的translate(),save()
  10. 利用View.inflate加载xml