Android中的px,in,pt,dp,dip,sp

本文深入探讨了Android平台下UI设计中分辨率独立测量单位的重要性,包括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标签的资源。
不加任何标签的资源是各种分辨率情况下共用的。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值