px"、 dip"、 dp"有什么区别?和 sp"?

本教程将介绍“px"、“dip"、“dp"有什么区别?和“sp"?的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

px"、 dip"、 dp"有什么区别?和 sp"? 教程 第1张

问题描述

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 中的密度独立性:

密度桶屏幕密度物理尺寸像素大小
ldpi120 dpi0.5 x 0.5 英寸0.5 英寸 * 120 dpi = 60x60 像素
mdpi160 dpi0.5 x 0.5 英寸0.5 英寸 * 160 dpi = 80x80 像素
hdpi240 dpi0.5 x 0.5 英寸0.5 英寸 * 240 dpi = 120x120 像素
xhdpi320 dpi0.5 x 0.5 英寸0.5 英寸 * 320 dpi = 160x160 像素
xxhdpi480 dpi0.5 x 0.5 英寸0.5 英寸 * 480 dpi = 240x240 像素
xxxhdpi640 dpi0.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 what sp actually stands for, one doc says "scale-independent pixels", the other says "scaleable pixels".

From Understanding Density Independence In Android:

Density BucketScreen DensityPhysical SizePixel Size
ldpi120 dpi0.5 x 0.5 in0.5 in * 120 dpi = 60x60 px
mdpi160 dpi0.5 x 0.5 in0.5 in * 160 dpi = 80x80 px
hdpi240 dpi0.5 x 0.5 in0.5 in * 240 dpi = 120x120 px
xhdpi320 dpi0.5 x 0.5 in0.5 in * 320 dpi = 160x160 px
xxhdpi480 dpi0.5 x 0.5 in0.5 in * 480 dpi = 240x240 px
xxxhdpi640 dpi0.5 x 0.5 in0.5 in * 640 dpi = 320x320 px
UnitDescriptionUnits Per Physical InchDensity Independent?Same Physical Size On Every Screen?
pxPixelsVariesNoNo
inInches1YesYes
mmMillimeters25.4YesYes
ptPoints72YesYes
dpDensity Independent Pixels~160YesNo
spScale Independent Pixels~160YesNo

More info can be also be found in the Google Design Documentation.

好了关于“px"、“dip"、“dp"有什么区别?和“sp"?的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。