安卓:'DP'到'像素'转换? [英] Android: 'dp' to 'px' conversion?
问题描述
我读这篇文章: http://developer.android.com/guide/做法/ screens_support.html
它说,该公式Android使用到的 DP 单元之间转换为 PX 单位如下:
It says that the formula Android uses to convert between a dp unit to a px unit is the following:
px = dp * (dpi / 160)
这篇文章还举了一个例子,当DPI为240,这使我们的PX = 1.5(我在计算一个单个DP像素)
The article also gives an example when the dpi is 240, which gives us px = 1.5 (I'm calculating for one single dp pixel)
然而,究竟是什么意思1.5在这里?一旦PX的单位实际上是物理设备的像素,将Android绘制1或2个像素?
However, what exactly does 1.5 mean here? Once the px units are actually the physical device pixels, will Android draw 1 or 2 pixels?
推荐答案
这取决于上下文。
如果在 DP 的值被用在上下文暗示的大小,就像的android:layout_width
属性,<一个描述的逻辑href="http://developer.android.com/reference/android/content/res/Resources.html#getDimensionPixelSize%28int%29">Resources.getDimensionPixelSize()会被用到。即, PX 的值将被舍入到最近的整数值,可使用特殊的情况下,如果 PX 的> 0,则实际值将是至少为1
If the dp value is used in a context that implies size, like the android:layout_width
attribute, the logic described for Resources.getDimensionPixelSize() will be used. That is, the px value will be rounded to the nearest integer value, with the special case that if px > 0, then the actual value will be at least 1.
如果在 DP 的值被用在一个背景下,意味着失调,如安卓insetLeft
中的插图绘制对象的属性,逻辑描述为<一个href="http://developer.android.com/reference/android/content/res/Resources.html#getDimensionPixelOffset%28int%29">Resources.getDimensionPixelOffset()会被用到。即, PX 的值将被简单地截断为一个整数值。
If the dp value is used in a context that implies offset, like the android:insetLeft
attribute of the Inset Drawable, the logic described for Resources.getDimensionPixelOffset() will be used. That is, the px value will simply be truncated to an integer value.
有时候未修改的浮点值时,如为安卓dashWidth
的属性&LT;中风/&GT;
的形状可绘制标签,但是这是pretty的罕见。一般规模或偏移逻辑的情况下,即使可以使用的浮点值。
Sometimes the unmodified floating point value is used, such as for the android:dashWidth
attribute of the <stroke/>
tag in a Shape Drawable, but this is pretty rare. Usually either the size or the offset logic is used, even if the floating point value could be used.
这篇关于安卓:'DP'到'像素'转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!