首页 > 代码库 > px dip dp dpi density sp 一次性弄懂

px dip dp dpi density sp 一次性弄懂

1 px  像素单位 像素和设备有关   如480 * 320分辨率的手机,则横向480个像素点,纵向320个像素点. 固定长度下像素点越多,则像素更高,像素点更小,图像更清晰

2. dpi :dots per inch  每英寸像素个数,常见取值 120,160,240,320。分别对应low,medium,high,xhigh, dpi取值,和设备有关(同样大小的手机分辨率有高有底),如     320* 240的手机,尺寸为 2.0 * 1.5(英寸) ,dpi则为160  。 通常广告所说的手机尺寸,如全新5寸大屏手机只要599你还在等什么。等等。这里的尺寸指的手机屏幕对   角线尺寸。勾股定理可换算得长宽尺寸。一般比例规则为4:3或16:9.

3. dip:Density independent pixels ,设备无关像素。

官方介绍:

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.

  抽象意义上的像素   dip和像素的关系: 1dip = (当前手机dpi/160 )px ,如上面dpi为240的手机,1dip相当于1.5px, 也就是说不同的手机1dip等同于不同数量的px.  如dpi为320的手机,则 1dip相当于2px.    google通过定义dip这样的抽象单位, 开发人员使用dip作为单位开发的控件在不同的手机上的大小是一样的。 如果使用px作为单位,可以想象在dpi低,即同样尺寸像素点相对稀疏的屏幕上,控件将会比dpi高的屏幕上更大。(因为需要暂用同样多的px,如给button设置为10px).

4. dp 就是 dip

5.   density: 当前手机dpi和标准dpi(160 px/inch)的比值        如480 * 320分辨率的手机 尺寸为2.0*1.5.  则dpi为240.     desity = 240/160 = 1.5

 

getContext().getResources().getDisplayMetrics().density 获得的即为density,即上面的值1.5

mDensity = getContext().getResources().getDisplayMetrics().densityDpi获得的为dpi值。即上面的240

 

getContext().getResources().getDisplayMetrics().density文档 这句话非常关键

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).

 

参考:http://www.cnblogs.com/yaozhongxiao/archive/2014/07/14/3842908.html

px dip dp dpi density sp 一次性弄懂