简体   繁体   English

Android - dp 如何在不同尺寸的设备中占据不同的空间?

[英]Android - How does dp occupy different space in devices with different sizes?

I searched a lot about dp, px, inch, pt etc.我搜索了很多关于 dp、px、inch、pt 等的信息。

From what I understood about 'dp':从我对“dp”的理解来看:

Specifying 'dp' is simply a way to make Android draw the views with same size for devices with different screen densities.指定 'dp' 只是一种让 Android 为不同屏幕密度的设备绘制相同大小视图的方法。 Eg, for a medium density device, each 'dp' will occupy a pixel.例如,对于中等密度的设备,每个“dp”将占据一个像素。 For a high density device - which has smaller pixels to fit more pixels per inch, 'dp' will occupy more than a pixel.对于高密度设备 - 它具有较小的像素以每英寸容纳更多像素,'dp' 将占用超过一个像素。 For a low density device - which has larger pixels to fit less pixels per inch, 'dp' will occupy less than a pixel.对于低密度设备 - 它具有较大的像素以适应每英寸较少的像素,'dp' 将占用不到一个像素。

But what I also read is that space occupied by a 'dp' varies according to screen sizes, ie for a small screen a 'dp' will occupy less space, while for a larger screen a 'dp' will occupy larger space.但我还了解到,“dp”占用的空间因屏幕尺寸而异,即对于小屏幕,“dp”将占用更少的空间,而对于更大的屏幕,“dp”将占用更大的空间。 How exactly does this happen?这究竟是怎么发生的? From what I understood about 'dp', it should occupy the same amount of space in devices with differing screen sizes.根据我对“dp”的理解,它应该在不同屏幕尺寸的设备中占据相同的空间。 Eg, a large screen of 240 dpi will have a 'dp' occupy 1.5 pixels, and so will be the case for a small screen of 240 dpi.例如,240 dpi 的大屏幕将有一个 'dp' 占据 1.5 像素,240 dpi 的小屏幕也是如此。 Then how is it that a 'dp' will occupy different amount of space for different screen sizes?那么一个“dp”怎么会在不同的屏幕尺寸下占据不同的空间呢?

What am I missing in my understanding of 'dp'?我对“dp”的理解缺少什么? Please help.请帮忙。

on the Android operating system a device-independent pixel is equivalent to one physical pixel on a 160 dpi screen.在 Android 操作系统上,独立于设备的像素相当于 160 dpi 屏幕上的一个物理像素。 By this definition, you can understanddp has no relation with screen sizes as the scale is already fixed as 160 dpi.通过这个定义,你可以理解 dp 与屏幕尺寸没有关系,因为比例已经固定为 160 dpi。

. .

Android defines a baseline dpi of 160 which is used as the reference to compute sizes for all screen densities: Android 定义了160的基准 dpi,用作计算所有屏幕密度的尺寸的参考

pixel_size * (device_dpi / baseline_dpi) = result in "dp" units

>> or the other way around 

dp_size / (device_dpi / baseline_dpi) = result in "pixel" units

Therefore, 1 pixel in a 240dpi device is equivalent to 1.5dp units:因此,240dpi 设备中的 1 个像素相当于 1.5dp 单位:

1 * (240 / 160) = 1.5

and the other way around, 1.5dp units in a 240dpi device is equivalent to 1 pixel反之亦然,240dpi 设备中的 1.5dp 单位相当于 1 个像素

1.5 / (240 / 160) = 1

The important fact to know is that 160 is the baseline used as the reference for all DPIs.要知道的重要事实是, 160是用作所有 DPI参考的基线。 So, as dp units increase/decrease, the required pixels area to draw something translates into keeping the same size scale regardless of the device screen.因此,随着 dp 单位的增加/减少,无论设备屏幕如何,绘制内容所需的像素区域都会转化为保持相同的大小比例。

More information in the official documentation .更多信息请参见官方文档

To be more clear:更清楚一点:

The display size is not related to "dp units".显示大小与“dp 单位”无关。 The display size is just how big the display canvas is.显示大小就是显示画布的大小。 The screen DPI defines how many dots fit in 1 square inch.屏幕 DPI 定义了 1 平方英寸可容纳多少点。 And a "dp unit" is an abstract unit that, depending on the device's DPI, is scaled (up or down) to give "uniform dimensions" on any screen size, by using 160 as the baseline reference.并且“dp 单位”是一个抽象单位,根据设备的 DPI,通过使用 160 作为基准参考,缩放(向上或向下)以在任何屏幕尺寸上提供“统一尺寸”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM