Android: 'dp' to 'px' conversion?

前端 未结 4 1405
失恋的感觉
失恋的感觉 2021-02-13 20:47

I am reading this article: http://developer.android.com/guide/practices/screens_support.html

It says that the formula Android uses to convert between a dp

4条回答
  •  梦如初夏
    2021-02-13 21:03

    It depends on the context.

    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.

    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.

    Sometimes the unmodified floating point value is used, such as for the android:dashWidth attribute of the 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.

提交回复
热议问题