Search code examples
androiddensity-independent-pixel

How do I convert to DIP?


What is the formula for converting regular pixels to DIP?

Assuming I have a photoshop document with a design in it that was intended for the Galaxy Tab (for example) which is 600x1024 actual pixels.
What is the ratio between those and the DIP? I'm unclear on how Android translates this.

I want to use DIP (rather than pixels) so that it scales to look "ok" on other devices, but my primary concern is to get it pixel perfect in this resolution and my aim is to measure the position of an element in photoshop and then get an exact translation as to what the DIP needs to be for it to lay out identically on the Tab.


Solution

  • Their formula is somewhere in the docs:

     truePixels = DIPs * (device DPI / 160) 
    

    Ah, there it is:

    The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.

    Taken from here.