为什么" DP到像素"比率成正比与屏幕密度变化,但不一定 [英] Why "dp-to-pixel" ratio changes with the screen density, but not necessarily in direct proportion

查看:226
本文介绍了为什么" DP到像素"比率成正比与屏幕密度变化,但不一定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点被我读担心这里 ,哪种,都意味着没有可靠的公式计算像素根据 DP 和屏幕密度(副反之亦然):


  

DP:
     密度独立像素 - 即是基于屏幕的物理密度的抽象单元。这些单位是相对于160 DPI(每英寸点数)的屏幕,其上1DP大致相等为1px。当一个较高密度屏幕上运行,用于绘制1DP像素的数量是由一个因子相应于屏幕的dpi的按比例放大。同样,较低的密度在屏幕上时,用于1DP像素的数量按比例缩小。 的DP到像素将与屏幕密度不一定成正比改变,但比。使用dp单位(而不是PX单位)是一个简单的解决方案,在布局使得视图尺寸适当调整针对不同屏幕密度。换句话说,它提供了一致性为在不同设备的UI元素的真实世界的尺寸。


我以为我们可以一直用下面的公式(这里解释):
PX = DP *(DPI / 160)

不过,在<一个见证href=\"http://stackoverflow.com/questions/9906520/android-which-ui-element-size-unit-to-use-if-dp-not-working\">this问题似乎其中 DP 指定的大小并不能保证跨设备固定大小知觉突出的例子。

什么是大胆句子的真正含义?
我们是否需要使用毫米为了一定要保持相同的感知大小??


解决方案

  

我以为我们可以一直用下面的公式(在这里解释):像素= DP *(DPI / 160)


此公式可以随时用来像素和DP之间的转换。只要确保你使用 DisplayMetrics.densityDpi 为DPI值在公式


  

然而,在这个问题上的证词似乎突出显示在指定的DP大小并不能保证跨设备固定大小知觉的情况。


是的,这是事实,一定DP并不能保证的固定感知跨设备的尺寸。然而,它保证的类似感知跨设备的尺寸。这样做的原因是,在某些设备上densityDpi(密度斗)是不同比的物理DPI 。请参阅我的回答给你更多的细节联系在一起的问题。


  

的DP到像素的比例将与屏幕密度改变,但不一定成正比


在这里,我想他们的意思是实际的物理屏幕密度,当他们说屏幕密度,而不是手机属于哪个密度桶。

例如用270的物理dpi的一个装置将属于高(240)密度桶和具有一百六分之二百四十= 1.5 DP到像素比

与290的物理dpi的另一个设备将属于xhigh(320)密度桶和具有一百六分之三百二十〇一个DP到像素比= 2

在这些设备之间比较相互物理屏幕的密度也增加了7%(270 - > 290),而DP-到像素的比率增加了33%(1.5 - > 2),所以它不是一个直接的它们之间的比例。


  

我们需要以使用毫米一定要保持相同的感知大小??


如果是重要的preceived大小尽可能你必须使用毫米或英寸设备之间的相似。然而,这有一定的缺点,因为我在你链接的问题提了。

同样重要的是要记住,有一些设备,其中毫米和英寸为单位被打破,更多的信息,看到这个问题的Why使用lint时显示警告(英寸)或毫米(毫米)的单位尺寸?

I am a bit worried by what I read here, which kind-of implies that there is no reliable formula to compute px based on dp and screen density (and vice versa):

dp : Density-independent Pixels - An abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen's dpi. Likewise, when on a lower density screen, the number of pixels used for 1dp is scaled down. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Using dp units (instead of px units) is a simple solution to making the view dimensions in your layout resize properly for different screen densities. In other words, it provides consistency for the real-world sizes of your UI elements across different devices.

I thought we could always use the following formula (explained here) : px = dp * (dpi / 160)

However, the testimony in this question seems to highlight a case where specifying sizes in dp does not guarantee a fixed perceived size across devices.

What is the real meaning of the sentence in bold ? Do we need to use mm in order to be sure to keep the same perceived size ??

解决方案

I thought we could always use the following formula (explained here) : px = dp * (dpi / 160)

This formula can always be used to convert between pixels and dp. Just make sure you use DisplayMetrics.densityDpi as the value for dpi in the formula.

However, the testimony in this question seems to highlight a case where specifying sizes in dp does not guarantee a fixed perceived size across devices.

Yes it is true that a certain dp does not guarantee a fixed perceived size across devices. It does however guarantee a similar perceived size across devices. The reason for this is that on some devices densityDpi (the density bucket) is different than the physical dpi of the device. Please see my answer to the question you linked for more details.

The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion

Here I think they mean the actual physical screen density when they say "screen density" and not which density bucket the phone belongs to.

For example a device with a physical dpi of 270 will belong to the "high" (240) density bucket and have a dp-to-pixel ratio of 240/160 = 1.5

Another device with a physical dpi of 290 will belong to the "xhigh" (320) density bucket and have a dp-to-pixel ratio of 320/160 = 2

When you compare these devices with each other the physical screen density has increased by 7% (270 -> 290) while the dp-to-pixel ratio has increased by 33% (1.5 -> 2) so it's not a direct proportion between them.

Do we need to use mm in order to be sure to keep the same perceived size ??

If it's important that the preceived size is as similar as possible between devices you have to use mm or inch. However this have some drawbacks as I mention in the question you linked.

It is also important to keep in mind that there are some devices where the mm and inch units are broken, for more information on this see the question Why Lint shows warning when using in (inch) or mm (millimeter) units as dimension?

这篇关于为什么&QUOT; DP到像素&QUOT;比率成正比与屏幕密度变化,但不一定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆