[转帖]关于android的单位dp,dip_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2636 | 回复: 0   主题: [转帖]关于android的单位dp,dip        下一篇 
    本主题由 koei123 于 2015-6-1 15:16:10 移动
sysbobo
注册用户
等级:新兵
经验:46
发帖:66
精华:0
注册:2012-2-21
状态:离线
发送短消息息给sysbobo 加好友    发送短消息息给sysbobo 发消息
发表于: IP:您无权察看 2015-3-24 8:57:55 | [全部帖] [楼主帖] 楼主

      Android中,dp是Density-independent Pixels简写,而dip则是Density Independent Pixels,实际上指的是一个概念,都是Android的单位。 在文档More Resource Types中有描述:dp Density-independent Pixels - An abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen's dpi. Likewise, when on a lower density screen, the number of pixels used for 1dp is scaled down. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Using dp units (instead of px units) is a simple solution to making the view dimensions in your layout resize properly for different screen densities. In other words, it provides consistency for the real-world sizes of your UI elements across different devices. 单位px代表的是像素,它和ds的转换公式在文档Supporting Multiple Screens中也有描述: Density-independent pixel (dp) A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way. The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities. 公式px = dp * (dpi / 160) 说明1dp代表了多少个px,下面我们简单的推导一下这个公式。 首先,dpi是Dots Per Inch的缩写(wiki),表示一英寸内有多少个点,对于UI界面而言,这个点就是像素(Pixel)。然后,我们看一下dp的单位像素 = dp的单位 * 像素/英寸 可以看出dp的单位是长度单位英寸,上面dp的定义的最后一句话说明了这一点:In other words, it provides consistency for the real-world sizes of your UI elements across different devices. 最后,我们看一下1dp的值有多大。1dp对应dpi/160个像素,根据dpi的定义,每个像素的长度为1/dpi英寸,合在一起就是1dp=1/160英寸。 1dp的长度 = 1 * dpi / 160 * 1 / dpi 英寸 = 1 / 160 英寸 结论 dp等同于dip,它是一个长度单位,1dp=1/160英寸。 dp这个单位的意义在于:你可以根据看到的实际大小,在应用中设置对应的dp值,而使得不同的设备上看起来一样大。

--转自 北京联动北方科技有限公司



该贴由koei123转至本版2015-6-1 15:16:10



赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论