原文地址:http://blog.csdn.net/chenyufei1013/article/details/8363619

  • 摘要

本文介绍了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.

公式

  1. px = dp * (dpi / 160)

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

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

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

  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英寸。

  1. 1dp的长度 = 1 * dpi / 160 * 1 / dpi 英寸 = 1 / 160 英寸
  • 结论

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

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

(转)dp和dip是同一个单位的更多相关文章

  1. android中dx、dp、dip、sp单位的区别

    1.dp=dip 2.px基于像素,后两者基于像素密度. 3.px既可用于宽度高度,也可用于字体,dp用于宽高,sp用于字体4.android中以320*480屏幕为基准.在相同值的px和dp,在32 ...

  2. Android中如何将dp,dip,sp与px相互转化

    Android中有很多度量单位:比如常用的dp,dip,sp,px等,有时候需要将他们相互转换,有下面非常方便的方法: 比如sp转换成px: TypedValue.applyDimension(Typ ...

  3. px、dp和sp,这些单位有什么区别?

    DP 这个是最常用但也最难理解的尺寸单位.它与“像素密度”密切相关,所以 首先我们解释一下什么是像素密度.假设有一部手机,屏幕的物理尺寸为1.5英寸x2英寸,屏幕分辨率为240x320,则我们可以计算 ...

  4. Android dp、dip、dpi、px、sp简介及相关换算,及其应用实例

    屏幕分辨率:在x y轴上的像素点数.单位是px,1px=1个像素点.一般以    纵向像素×横向像素    表示,如1920*1080dpi--------------------------每英寸上 ...

  5. [Android问答] px、dp和sp,这些单位有什么区别?

    相信每个Android新手都会遇到这个问题,希望这篇帖子能让你不再纠结. px: 即像素,1px代表屏幕上一个物理的像素点: px单位不被建议使用,因为同样100px的图片,在不同手机上显示的实际大小 ...

  6. android应用开发之View的大小计量单位(px、dpi、dp、dip、sp)

    http://blog.csdn.net/ljianhui/article/details/43601495?ref=myread 一.像素(px)与屏幕分辨率 1)px(Pixels ,像素):对应 ...

  7. dp\dpi\px\pt\em单位长度理解

    屏幕都有固定的物理长宽度属性和分辨率 比如电脑.比如手机屏幕 例如有手机屏幕尺寸是1.5英寸x2英寸,屏幕分辨率为240x320, 那么可以推算水平方向每英寸的像素数(dpi)是:240/1.5=16 ...

  8. Android原生系统API自带dp、px、sp单位转换

    Android系统中自带的Api中可以使用TypedValue进行单位转换 1,调用系统api转换单位 // 获得转换后的px值 float pxDimension = TypedValue.appl ...

  9. [置顶] Android 适配真要命?

    原始尺寸场景 相信大家对上面也有所有耳闻另外就是如何计算屏幕的密度一般都是按照勾股定理例如中等屏幕密度 480^2+800^2开根号 然后除以当前屏幕尺寸3.5-4.2之间尺寸. 对于刚出来的那些An ...

随机推荐

  1. UML九种图作用简介

    UML(统一建模语言):是面向对象的可视化建模语言. UML中有3种构造块:事物.关系和图,事物是对模型中最具有代表性的成分的抽象,关系是把事物结合在一起,图聚集了相关的事物 UML中有九种图如下: ...

  2. 总结javascript中的全局变量和局部变量的区别以及声明函数和调用函数的区别

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="utf-8&quo ...

  3. oracle基本操作符/运算符/操作语言

    racle中的操作符算术操作符: 无论是在sqlserver,或者是java中,每种语言它都有算术操作符,大同小异.Oracle中算术操作符(+)(-)(*)(/) 值得注意的是:/ 在oracle中 ...

  4. 十分钟轻松让你认识ASP.NET MVC6

    这篇文章说明下如何在普通编辑器下面开发mvc6应用程序. 上篇文章: 十分钟轻松让你认识ASP.NET 5(MVC6) 首先安装mvc6的nuget包: 可以看到在project.json文件中添加了 ...

  5. web 前端常用组件【06】Upload 控件

    因为有万恶的IE存在,所以当Web项目初始化并进入开发阶段时. 如果是项目经理,需要知道客户将会用什么浏览器来访问系统. 明确知道限定浏览器的情况下,你才能从容的让手下的封装必要的前端组件. 本篇文章 ...

  6. C#进阶系列——MEF实现设计上的“松耦合”(一)

    前言:最近去了趟外地出差,介绍推广小组开发的框架类产品.推广对象是本部门在项目上面的同事——1到2年工作经验的初级程序员.在给他们介绍框架时发现很多框架设计层面的知识他们都没有接触过,甚至没听说过,这 ...

  7. 秀起来的coding

    转载:http://www.zuidsaima.com/html/1695882735602688/index.html

  8. [转]java selenium webdriver实战 应用小结

    原文链接:http://www.cnblogs.com/itliucheng/p/5578788.html 部分api 1.访问网站 driver.get("http://www.baidu ...

  9. Android四大组件之—— 使用服务进行后台操作

    什么是服务 服务是一个没有可视化界面的组件,它可以在后台长期运行并进行各种操作. 服务的创建 我们只需要继承Service类并实现相应的方法即可创建服务 要想启动服务,还得在AndroidManife ...

  10. C#稳固基础:传统遍历与迭代器

    Hello,Coders.我们除了天天的码 if…else…之外,还会不断的码出foreach.我今天要说的是:传统遍历需实现的接口及我们还有一种更简洁优雅的方式实现多种迭代器. 传统遍历 传统的遍历 ...