px"、 dip"、 dp"有什么区别?和 sp"?
本教程将介绍“px"、“dip"、“dp"有什么区别?和“sp"?的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。
问题描述
Android 计量单位有什么区别?
像素
浸
dp
sp
解决方案
来自Android 开发者文档:
像素
>像素 - 对应于屏幕上的实际像素.
在
>英寸 - 基于屏幕的物理尺寸.
>1 英寸 = 2.54 厘米
mm
>毫米 - 基于屏幕的物理尺寸.
pt
>点 - 基于屏幕物理尺寸的 1/72 英寸.
dp 或 dip
>密度-独立像素 - 基于屏幕物理密度的抽象单位.这些单位是相对于 160dpi 屏幕,所以 1 dp 是 160 dpi 屏幕上的一个像素.dp与像素的比例会随着屏幕密度而变化,但不一定成正比.注意:编译器同时接受dip".和dp",尽管dp"与sp"更一致.
sp
>可缩放像素 OR 与缩放无关的像素 - 这类似于 dp 单位,但它也按比例缩放用户的字体大小偏好.建议你指定字体大小时使用此单位,因此它们将被调整对于屏幕密度和用户的偏好.请注意,Android 文档与 sp
实际代表的含义不一致,一个 doc 说与比例无关的像素",其他表示可缩放像素".
来自 了解 Android 中的密度独立性:
密度桶 | 屏幕密度 | 物理尺寸 | 像素大小 |
---|---|---|---|
ldpi | 120 dpi | 0.5 x 0.5 英寸 | 0.5 英寸 * 120 dpi = 60x60 像素 |
mdpi | 160 dpi | 0.5 x 0.5 英寸 | 0.5 英寸 * 160 dpi = 80x80 像素 |
hdpi | 240 dpi | 0.5 x 0.5 英寸 | 0.5 英寸 * 240 dpi = 120x120 像素 |
xhdpi | 320 dpi | 0.5 x 0.5 英寸 | 0.5 英寸 * 320 dpi = 160x160 像素 |
xxhdpi | 480 dpi | 0.5 x 0.5 英寸 | 0.5 英寸 * 480 dpi = 240x240 像素 |
xxxhdpi | 640 dpi | 0.5 x 0.5 英寸 | 0.5 英寸 * 640 dpi = 320x320 像素 |
单位 | 说明 | 每物理英寸单位数 | 密度无关? | 每个屏幕的物理尺寸都一样? |
---|---|---|---|---|
像素 | 像素 | 变化 | 没有 | 没有 |
在 | 英寸 | 1 | 是的 | 是的 |
mm | 毫米 | 25.4 | 是的 | 是的 |
点 | 积分 | 72 | 是的 | 是的 |
dp | 与密度无关的像素 | ~160 | 是的 | 没有 |
sp | 缩放独立像素 | ~160 | 是的 | 没有 |
还可以在 Google 设计文档中找到更多信息.
What is the difference between Android units of measure?
px
dip
dp
sp
解决方案
From the Android Developer Documentation:
px
> Pixels - corresponds to actual pixels on the screen.
in
> Inches - based on the physical size of the screen.
> 1 Inch = 2.54 centimeters
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 or dip
> 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
> Scaleable Pixels OR scale-independent pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommended you
use this unit when specifying font sizes, so they will be adjusted
for both the screen density and the user's preference. Note, the Android documentation is inconsistent on whatsp
actually stands for, one doc says "scale-independent pixels", the other says "scaleable pixels".
From Understanding Density Independence In Android:
Density Bucket | Screen Density | Physical Size | Pixel Size |
---|---|---|---|
ldpi | 120 dpi | 0.5 x 0.5 in | 0.5 in * 120 dpi = 60x60 px |
mdpi | 160 dpi | 0.5 x 0.5 in | 0.5 in * 160 dpi = 80x80 px |
hdpi | 240 dpi | 0.5 x 0.5 in | 0.5 in * 240 dpi = 120x120 px |
xhdpi | 320 dpi | 0.5 x 0.5 in | 0.5 in * 320 dpi = 160x160 px |
xxhdpi | 480 dpi | 0.5 x 0.5 in | 0.5 in * 480 dpi = 240x240 px |
xxxhdpi | 640 dpi | 0.5 x 0.5 in | 0.5 in * 640 dpi = 320x320 px |
Unit | Description | Units Per Physical Inch | Density Independent? | Same Physical Size On Every Screen? |
---|---|---|---|---|
px | Pixels | Varies | No | No |
in | Inches | 1 | Yes | Yes |
mm | Millimeters | 25.4 | Yes | Yes |
pt | Points | 72 | Yes | Yes |
dp | Density Independent Pixels | ~160 | Yes | No |
sp | Scale Independent Pixels | ~160 | Yes | No |
More info can be also be found in the Google Design Documentation.
好了关于“px"、“dip"、“dp"有什么区别?和“sp"?的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。