首页 > 解决方案 > Conversion of dp to px and px to dp. Conceptual misunderstanding.

问题描述

I am a bit confused regarding the conversion of dp to px and px to dp. The formula goes this way

px = dp*(dpi/160)

Let's consider two screens A & B of densities of 160 dpi and 320 dpi respectively. This itself means that in the same area B has more number of pixels than A which means the pixel size of 160dpi screen should be larger for the same screen size and should be equal to the physical size of 2 pixels of 320dpi screen. But, putting in the formula for B this gives that 1 pixel for B is equivalent to the physical size of 2 density independent pixels or 2 pixels of 160dpi screen. What am I missing here?

标签: androidandroid-layout

解决方案


尝试通过此示例了解缩放系统。假设您有一个要在每个屏幕上显示48dp x 48dp大的位图(与密度无关)。我们有:

  • mDPI,160dpi 屏幕(比率1x)--> 位图:48x48 px(旧设备)
  • hDPI,240dpi 屏幕(比例1.5x)--> 位图:72x72 px (48 x 1.5)
  • xhDPI,320dpi 屏幕(比率2x)--> 位图:96x96 像素(48 x 2)
  • xxhDPI,480dpi 屏幕(比例3x)--> 位图:144x144 像素(48 x 3)
  • xxxhDPI,640dpi 屏幕(比例4x)--> 位图:192x192 px (48 x 4)

简而言之,要回答您的问题,不同密度的像素数量不同,但dps数量相同。


推荐阅读