Android中的px,in,pt,dp,dip,sp
Historically, programmers always designed UI in terms of pixels. For example, you might make a field 300 pixels wide, allow 5 pixels of spacing between columns, and define icons 16-by-16 pixels in size. The problem is that if you run that program on new displays with more and more dots(pixels) per inch (dpi), the UI appears smaller and smaller.
Resolution-independent measurements help solve this problem. Android supports all the following units:
• px (pixels): Dots on the screen.
• in (inches): Size as measured by a ruler.
• pt (points): 1/72 of an inch.
• dp (density-independent pixels): An abstract unit based on the density of the screen. On a display with 160 dots per inch, 1dp = 1px.
• dip: Synonym for dp, used more often in Google examples.
• sp (scale-independent pixels): Similar to dp, but also scaled by the user’s font size preference.
To make your interface scalable to any current and future type of display, I recommend you always use the sp unit for text sizes and the dip unit for everything else. You should also consider using vector graphics instead of bitmaps
px:像素点
in:英寸
pt:磅,1/72 英寸
density:表示每英寸有多少个dots(pixels)
dp:一个基于density的抽象单位,如果一个160dpi的屏幕,1dp=1px
dip:等同于dp
sp:同dp相似,但还会根据用户选定的字体大小来缩放。
建议使用sp作为文本的单位,其它用dip。
dp与px的换算:(必须知道所用屏幕的density)
px = (density / 160) * dp
说白了,dp就是选定了160 pixel per inch的density为基准,可以根据实际的density自动scale到实际使用的pixel。
HVGA屏density=160;
QVGA屏density=120;
WVGA屏density=240;
WQVGA屏density=120
当屏幕density=240时,使用hdpi 标签的资源
当屏幕density=160时,使用mdpi标签的资源
当屏幕density=120时,使用ldpi标签的资源。
不加任何标签的资源是各种分辨率情况下共用的。
更多相关文章
- Android学习——uses-sdk标签详解
- Android系统信息获取 之十二:获取屏幕分辨率及密度
- Andriod界面设计适配和Android Studio中的资源
- Android屏幕适配全攻略(最权威的官方适配指导)Android屏幕适配出现
- Android之应用资源的调用
- Android 1.6 支持更多的屏幕大小和分辨率
- Android屏幕计量单位详解
- Android 基于dpi的资源加载