How do I convert ppi into dpi for Android images?

Ruchira picture Ruchira · Aug 3, 2011 · Viewed 32.4k times · Source

I have started making graphics for my Android app using Adobe Photoshop. But I am unable to proceed, as the resolution in Photoshop is set in pixels per inch where as the official Google documentation says Android will require images set in dpi. I have searched the web for the conversion between the two but never ended up with any proper formula.

I know that the Android documentation describes the relation as px = dp*dpi/160. But my problem is that if I know dpi where do I get the value of dp to be used in this calculation? Or is there any assumption about the value of dp? I am confused.

Answer

Michael Allen picture Michael Allen · Aug 4, 2011

Dp are Density independant pixels and are used to generalise the number of pixels a screen has. These are generalised figures taken from http://developer.android.com/guide/practices/screens_support.html

  • xlarge screens are at least 960dp x 720dp
  • large screens are at least 640dp x 480dp
  • normal screens are at least 470dp x 320dp
  • small screens are at least 426dp x 320dp

Generalised Dpi values for screens:

  • ldpi Resources for low-density (ldpi) screens (~120dpi)
  • mdpi Resources for medium-density (mdpi) screens (~160dpi). (This is the baseline density.)
  • hdpi Resources for high-density (hdpi) screens (~240dpi).
  • xhdpi Resources for extra high-density (xhdpi) screens (~320dpi).

Therefore generalised size of your resources (assuming they are full screen):

  • ldpi
    • Vertical = 426 * 120 / 160 = 319.5px
    • Horizontal = 320 * 120 / 160 = 240px
  • mdpi
    • Vertical = 470 * 160 / 160 = 470px
    • Horizontal = 320 * 160 / 160 = 320px
  • hdpi
    • Vertical = 640 * 240 / 160 = 960px
    • Horizontal = 480 * 240 / 160 = 720px

Edit - adding xhdpi as they are becoming more popular

  • xhdpi
    • Vertical = 960 * 320 / 160 = 1920px
    • Horizontal = 720 * 320 / 160 = 1440px

These values should be suitable for most xhdpi screens such as TVs and the Nexus 4, including the Nexus 10 (assuming they don't create a new category for this as it is 25k x 16k, don't know as I haven't got hands on one yet).

/Edit


If you use these sizes your images will look great on any screen. Be sure to define sizes in code in dp however, Android will handle the conversion described above on its own.