关于android的单位dp,dip

  • 摘要

        本文介绍了android单位dp,dip的概念,并给出了它的确切含义。

  • 概述

        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.
        公式

[cpp]  view plain copy
  1. px = dp * (dpi / 160)  

        说明1dp代表了多少个px,下面我们简单的推导一下这个公式。

        首先,dpi是Dots Per Inch的缩写(wiki),表示一英寸内有多少个点,对于UI界面而言,这个点就是像素(Pixel)。

        然后,我们看一下dp的单位:

[plain]  view plain copy
  1. 像素 = 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英寸。

[cpp]  view plain copy
  1. 1dp的长度 = 1 * dpi / 160 * 1 / dpi 英寸 = 1 / 160 英寸  

  • 结论

        dp等同于dip,它是一个长度单位,1dp=1/160英寸。

        dp这个单位的意义在于:你可以根据看到的实际大小,在应用中设置对应的dp值,而使得不同的设备上看起来一样大。

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REaDME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值