📜  Android中的“px”、“dip”、“dp”和“sp”有什么区别?

📅  最后修改于: 2022-05-13 01:58:10.798000             🧑  作者: Mango

Android中的“px”、“dip”、“dp”和“sp”有什么区别?

XML 定义的维度值。尺寸由后跟测量单位的数字表示。例如,25px、5in、10dp 和 10sp。当您使用 sp/dp 时,您的 Android 应用程序将与各种屏幕密度和分辨率兼容。

  • PX:是Pixels的缩写,指定屏幕上的实际像素。
  • SP:是Scale无关像素的缩写。它与 dp 单位相同,但根据用户的字体大小选择额外缩放。
  • DP:一个虚拟像素单元,用于在创建 UI 布局时以与密度无关的方式传达布局尺寸或位置。与密度无关的像素对应于 160 dpi 屏幕上的一个物理像素,这是“中等”密度屏幕的系统基线密度。在运行时,系统会根据使用中的屏幕的实际密度以透明的方式处理所需的任何 dp 单位缩放。

术语 DP 和 DIP 指的是与密度无关的像素,它们基于屏幕的物理密度。

  • SP:与 dp 类似,但也根据用户的字体大小选择进行缩放。选择字体大小时,建议您使用此单位,以便根据屏幕密度和用户选择调整它们。

1. Android 设备的方向

用户可以看到屏幕的方向。这是横向或纵向,表示屏幕的纵横比是宽的还是高的。请注意,各种设备不仅在默认情况函数以不同的方向运行,而且当用户旋转设备时,方向可能会在运行时发生变化。

2.屏幕的密度

屏幕物理区域内的像素数;有时缩写为 dpi(每英寸点数)。例如,“低”密度屏幕在给定物理区域内包含的像素比“正常”或“高”密度屏幕少。为了便于使用,Android 将所有真实屏幕密度分为六种通用密度:低、中、高、超高、超超高和超超高。

3. 安卓设备分辨率

屏幕的全部物理像素。当提供多屏支持时,程序不应该关心分辨率;相反,它们应该关注屏幕尺寸和密度,如通用尺寸和密度组所述。

差异表

px (pixels) 

in (inches)

mm (millimeters) 

pt (points)

dp or dip (Density-independent Pixels)

Refers to the actual pixels on the screen.Depending on the actual size of the screen in inches.Determined by the actual size of the screen.Based on the actual size of the screen, 1/72 of an inch, assuming a screen with a resolution of 72dpiAn abstract unit based on on-screen physical density. These measurements are measured in relation to a 160 dpi screen.
The compiler only accepts pxThe compiler only accepts in (however this unit is not recommended because it may vary)The compiler takes mm, but not by default (takes sp)Not a familiar unit, used in old Android Programming, by Eclipse. Still, the compiler takes it.The Compiler accepts both “dip” and “dp,”
The actual physical present pixels of the screen is measured by its diagonal. Android combines all real screen sizes into four generic sizes for ease of use: small, standard, big, and extra-large.The actual screen estate which is available on the running device is accessible to the Android OS.The usual unit of measurement is millimeters, used like in everyday measurements.Similar to the dp unit, but scaled by the user’s font size selection. When choosing font sizes, it is suggested that you use this unit so that they are adjusted for both screen density and user choice.The dp-to-pixel ratio will alter with screen density, but not always in a straight proportion. However “dp” is more consistent with “sp.”
Can be used to determine the layouts and elements places on the layout.Can be used to scale the elements on the screen concerning the actual screen.Should not be used for setting and determining the layouts due to the variations possible. Can be used to design the layout just like px but is preferred less as compared to the other measurements. The “sp” here is only used for text; it should never be used for layout sizes.
px = dp * (dpi / 160)N/A (as the measurement is absolute)N/A (as the measurement is absolute)N/A (as the measurement is absolute)Density = sqrt((wp * wp) + (hp * hp)) / di
Represented in terms of x*y.
X is the horizontal axis, Y is the vertical.
Represented in absolute digits and integers within as suffixRepresented in absolute digits and integers within as mmRepresented as pts or pt depending on the version of Android Studio you are using.Represented as simply ‘dp’ and to use with text use ‘sp’.
NOT density-independentDensity IndependentDensity IndependentDensity IndependentDensity Independent

结论

高密度屏幕上的每英寸像素数高于低密度屏幕。因此,相同像素尺寸的 UI 组件在低密度屏幕上看起来更大,在高密度设备上看起来更小。与密度无关的像素,缩写为 dp(发音为“dips”),是在任何屏幕上具有一致大小的自适应单位。它们提供了一种通用方法来适应多个平台的设计。材质 UI 使用与密度无关的像素来确保组件在不同密度的显示器上始终显示。