1. 关于android多分辨率中的density和density-independent pixel的区别

为何要引入dip?

The reason for dip to exist is simple enough. Take for instance the T-Mobile G1. It has a pixel resolution of 320x480 pixels. Now image another device, with the same physical screen size, but more pixels, for instance 640x480. This device would have a higher pixel density than the G1.

—If you specify, in your application, a button with a width of 100 pixels, it will look at lot smaller on the 640x480 device than on the 320x480 device. Now, if you specify the width of the button to be 100 dip, the button will appear to have exactly the same size on the two devices.

如果定义一个button的宽度是100pixels,在640*480的设备中要比320*480的设备中小的多,而如果定义一个button的 宽度为100dip的话,在两个设备中尺寸大小几乎相同

—The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, the baseline density assumed by the platform (as described later in this document). At run time, the platform transparently handles any scaling of the dip units needed, based on the actual density of the screen in use. The conversion of dip units to screen pixels is simple: pixels = dips * (density / 160). For example, on 240 dpi screen, 1 dip would equal 1.5 physical pixels. Using dip units to define your application's UI is highly recommended, as a way of ensuring proper display of your UI on different screens

2. DIP 到 Pixel 的转换

由于设备的屏幕不一样,因此导致了不同机型的分辨率不一样。为了兼容多个分辨率,我们在 XML 中设置尺寸时一般应当使用 dip、sp 作为单位。

如果需要在运行时创建 View、调整 Layout,而这时 Android SDK 要求我们传入的参数都是 Pixel。为了保证兼容性,我们要把 dip 在运行时转换为 pixel。其代码如下

final static float scale = Resources.getSystem().getDisplayMetrics().density;
/**
* Converts DIP to pixels.
* @param dip the value of the dip.
* @return the value of the pixels.
*/
public static int dipToPixel(int dip) {
return (int) (dip * scale + 0.5f);
}


3. Difference of px, dp, dip and sp in android

px is one pixel. scale-independent pixels ( sp ) and density- independent pixels ( dip ) you want to use sp for font sizes and dip for everything
else dip==dp from here http://developer.android.com/guide/topics/resources/available-resources.html

px
Pixels - corresponds to actual pixels on the screen.

in
Inches - based on the physical size of the screen.

mm
Millimeters - based on the physical size of the screen.

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

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 screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: 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 user's preference.


更多相关文章

  1. 超过 50% 的 Android 设备依然运行 2.2 版本
  2. Android设备的ID
  3. Android获取设备唯一ID
  4. 狂刷Android范例之3:读写外部存储设备
  5. Android Training学习笔记之适配不同的设备
  6. 谷歌宣布Android设备累计激活量突破10亿台
  7. android获得屏幕高度和宽度

随机推荐

  1. drawable(hdpi,ldpi,mdpi)的区别
  2. 解决Android(安卓)应用方法数不能超过65K
  3. Android(安卓)WebView缓存策略详解
  4. Android(安卓)多屏幕支持
  5. Android(安卓)Studio运行安卓程序报Failu
  6. Android(安卓)9.0 sdCard文件读写
  7. Android动画:alpha、scale、translate、ro
  8. Android(安卓)studio插件GsonFormat 的使
  9. Unity Android真机调试
  10. Android(安卓)手势锁/锁屏/Pin解锁,一种精