android中的各种尺寸

来源:互联网 发布:数控编程员工资 编辑:程序博客网 时间:2024/05/01 11:20

转载自   http://www.2cto.com/kf/201111/109806.html


关于尺寸大小,主要有几个单位需要区分开来:dp,sp,pt,px,mm,in

 

px:pixel,像素大小单位。在android UI中这个单位和实际的物理屏幕分辨率一样,主要用来显示清晰度效果。

 

 

 

dp:Density-independent Pixels(设备独立像素),缩写是dp即dip,他是一个抽象的单位,由于物理的屏幕有大有小,但dp在不同的物理屏幕上显示的大小都是一样的,也就是,dp和屏幕大小无关,它只是一个抽象单位。在Android中可以用dip或dp表示,例如android:layout_width="88dp"。

 

在讲dip之前,我们应该先了解dpi的概念。DPI:Dots per Inch,即每英寸包含的点,一英寸约等于2.54cm,所以dpi的概念很容易在脑海中形成了。DPI是用来描述图像打印密度的单位,DPI越高,说明打印出来的图像越细腻清晰。在72dpi的计算机中dpi就是每英寸包含的像素数。dip是针对160dpi屏幕所定义的像素单位,即在160dpi的屏幕上,1px = 1dip. dp转化成px的计算公式是:px=dp*(density/160)。例如,对于同样比例的4英寸屏幕,一个是160dpi,一个是320dpi,那么20px在第一个屏幕中的宽度将是第二个中的两倍,这样就造成了比例失调,但如果是20dp,那么在第一块屏幕中将占20px,在第二块中占40px,所占屏幕宽度的比例是一样的。因此,dp消除了不同类型屏幕对布局的影响。

 

 

 

那这个dp到底有多大?官网文档解释说,这个dp单位是相对于一个160dpi即160分辨率的屏幕来说的(这里的分辨率的意思就是每英寸上点数),因此,我们说不管物理屏幕密度是多少,1英寸就等于160dp。如果分辨率是160dpi,即1英寸,根据上面的公式可知:

 

px=dp*(160dpi/160dpi)

 

px=dp*(160dp/160dp)

 

px=dp*1

 

即:1dp=1px

 

意思就是说:在160分辨率的屏幕上,一个dp就等于1个像素。

 

 

 

然后我们来看一个例子:

 

假如有一个手机A,他的分辨率是320*480px,然后B手机的分辨率是640*480px,A和B的尺寸是一样的,那我们知道B手机看起来更加清晰一些。假如我们在A和B中同时画一个按钮,我们定义按钮的宽度是100px,那么根据上面的计算公式,dp和分辨率成反比,那按钮在A手机中的dp要大,也就说我们在手机A的UI界面中看到的按钮要比B手机中的按钮大;如果我们定义按钮的宽度都是100dp,那么px和分辨率几乎成正比,分辨率越大,那px计算得到的值就越大,就表明看起来更加清晰,而不是看起来更大。所以我们说,dp与屏幕的物理尺寸无关,而px是用来标明是否看的更加清晰。

 

 

 

sp:Scale-independent Pixels,比例无关的像素。这个很像dp,但是主要用来描述字体大小的,因此,sp一般是用来设定字体大小。

 

 

 

pt:point,中文含义是点,表示物理屏幕1尺寸的1/72。

 

 

 

mm和in分别表示毫米和英寸,均表示屏幕的物理大小。

 

 

 

总结(个人理解):dp就是试图真正显示的大小,而px是清晰度。因此在android的视图设计中,要使用dp而不适用px。dp用法很广泛,sp主要用于字体,其他的很少用。

 

 

 

 

 

关于这几个尺寸官网原文是:

 

Dimension

 

 

 

A dimension value defined in XML. A dimension is specified with a number followed by a unit of measure. For example: 10px, 2in, 5sp. The following units of measure are supported by Android:

 

 

 

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, so 160dp is always one inch regardless of the screen density. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. You should use these units when specifying view dimensions in your layout, so the UI properly scales to render at the same actual size on different screens. (The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".)

 

sp

 

Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.

 

pt

 

Points - 1/72 of an inch based on the physical size of the screen.

 

px

 

Pixels - corresponds to actual pixels on the screen. This unit of measure is not recommended because the actual representation can vary across devices; each devices may have a different number of pixels per inch and may have more or fewer total pixels available on the screen.

 

mm

 

Millimeters - based on the physical size of the screen.

 

in

 

Inches - based on the physical size of the screen.

 

作者:飞扬云



原创粉丝点击