android屏幕的那些事之一!

前言,我写到后面发现这里有个帖子不错;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



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


 


转载于:https://my.oschina.net/banxi/blog/51293

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值