-2

正如 android 文档所说“将 dp 单位转换为屏幕像素很简单:px = dp * (dpi / 160)。例如,在 240 dpi 屏幕上,1 dp 等于 1.5 个物理像素。”

我对这个例子有点困惑。根据公式, px=(240/160)dp => px = 1.5dp 为什么会变成“在 240 dpi 的屏幕上,1 dp 等于 1.5 个物理像素”?它应该说 1 px 等于 1.5 dp。请让我清楚。

4

2 回答 2

2

The higher is the density, the smaller are the physical pixels. Therefore, to keep the size of a measure in dp to remains the same, 1dp requiert more physical pixels at a higher density because there are smaller.

The official normalisation for a dp is 1dp = 1px at a density of 160dpi; therefore, at a density of 240dpi - which is 50% greater - you need 50% more physical pixels in order to keep the same length for a measure expressed in dp.

于 2013-01-16T10:12:47.090 回答
1

Android 为不同屏幕密度的设备定义了密度比。对于 mdpi 设备,此比率设置为 1。因此,如果您将宽度指定为 1dp,android 会通过将 dp 值乘以密度比率来计算像素值,即对于 mdpi 设备,px = 1 (dp) * 1 (ratio) = 1px .

但是在 xhdpi 设备这样的高密度设备上,比率是 2,android 会通过将 dp 值乘以密度比率来将 dp 转换为像素。px = 1 (dp) * 2 (比率) = 2px。因此,您的元素将在 xhpi 设备上为 2px。

更多信息可以在这里找到:http ://www.jtechniques.com/android/android-basics/understanding-dp-in-android-ui-px-vs-dp

于 2014-04-23T14:04:52.657 回答