div、px、dpi 之间的关系

发布于 2024-10-08 21:31:07 字数 116 浏览 0 评论 0原文

如果在布局 xml 文件中,我将大小设置为,例如 12dip。 mdpi 中总是 12px,hdpi 中总是 18px 吗?

那么,dip 对于 mdpi 是否总是正确,并且对于其他密度也会相应地缩放?

If, in a layout xml file, I set the size to be, for example 12dip. Will that always be 12px in mdpi and so 18px in hdpi?

So is dip always true for mdpi and will scale accordingly for other densities?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东北女汉子 2024-10-15 21:31:07

官方文档完全涵盖了这个问题。 div、px 和 dpi 之间的关系由 this 介绍 部分。

引用:

与密度无关的像素 (dp)

应用程序可以使用它来定义其像素的虚拟像素单元
UI,用于表达布局尺寸或
以与密度无关的方式定位。

与密度无关的像素相当于屏幕上的一个物理像素。
160 dpi 屏幕,基线密度
平台假设(如描述
本文档稍后)。在运行时,
该平台透明地处理任何
所需 dp 单位的缩放,基于
屏幕的实际密度
使用。 dp单位换算为
屏幕像素很简单:像素= dps
*(密度/160)。例如,在 240 dpi 屏幕上,1 dp 等于 1.5
物理像素。使用 dp 单位
定义你的应用程序的 UI 是高度
建议,作为确保
在不同的平台上正确显示您的 UI
屏幕。

所以声明:

mdpi 中始终为 12px,hdpi 中始终为 18px

根据文档,

That question is fully covered by official documentation. Relations between dip, px and dpi are covered by this section.

Quote:

Density-independent pixel (dp)

A virtual pixel unit that applications can use in defining their
UI, to express layout dimensions or
position in a density-independent way.

The density-independent pixel is equivalent to one physical pixel on a
160 dpi screen, the baseline density
assumed by the platform (as described
later in this document). At run time,
the platform transparently handles any
scaling of the dp units needed, based
on the actual density of the screen in
use. The conversion of dp units to
screen pixels is simple: pixels = dps
* (density / 160). For example, on 240 dpi screen, 1 dp would equal 1.5
physical pixels. Using dp units to
define your application's UI is highly
recommended, as a way of ensuring
proper display of your UI on different
screens.

So the statement:

that always be 12px in mdpi and so 18px in hdpi

seems to be correct, according to the docs.

紫竹語嫣☆ 2024-10-15 21:31:07

在密度为 160 dpi 的设备上,12dp 为 12px。文档并没有说所有 mdpi 设备都具有 160 dpi,因此您可能会找到具有其他密度(例如 150 或 180 dpi)的 mdpi 设备。在这些情况下,关系 1dp = 1px 将不成立。

你只能确定这个关系:

px = dp * (dpi / 160)

如果设备的密度为 320 dpi,那么每个 dp 对应于 2 px,因为 320/160 是 2。我会说 2 是“密度因子”,但它也是您可以使用 getResources().getDisplayMetrics().densis 获得,因此也称为“密度”。

12dp will be 12px on a device with density 160 dpi. The docs don't say that all mdpi devices have exactly 160 dpi so it seems you might find mdpi devices with other densities (e.g. 150 or 180 dpi). In those cases, the relation 1dp = 1px would not be true.

You can only be sure of this relation:

px = dp * (dpi / 160)

If a device has a density of 320 dpi then each dp corresponds to 2 px, because 320/160 is 2. I would say that 2 is the "density factor", but it is also what you get with getResources().getDisplayMetrics().density, so it is also called "density".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文