Search code examples
androidpixeldensity-independent-pixel

How to compute device independent pixel units?


I am assuming that the terminology Density-independent pixels and Device-independent pixels, have same meaning.

Density-independent pixels(dip) are a virtual pixel unit, equivalent to one physical pixel on a 160dpi (MDPI) screen.

4dip is equivalant to 320dpi (XHDPI) screen, because the number of pixels used to render same data on 320dpi screen is 4(2 pixels across and 2 pixels down).

How do I compute pixel units in dip for 240dpi (HDPI) screen?


Solution

  • HDPI is x1.5 for MDPI.

    That is 160x1.5 = 240.

    On MDPI device, which is standard for dip unit, 1dip is 1px. On HDPI device, physically same size 1dip is 1.5px.

    When you at first know some px value, you can convert it into dip by deviding by its scale, for example, so HDPI's scale is 1.5 that you can devide px value by 1.5.

    For the same 2dip, HDPI has 3px across and down while MDPI has 2px across and down.

    2dip => 2px on MDPI
    2dip => 2 x 1.5 = 3px on HDPI
    

    The concept of dip is that 1 dip means physical size of 1/160 inch approxmately, it's a common physical size through all over the devices that have different pixel densities.