Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert ppi into dpi for Android images?

Tags:

android

dpi

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.

like image 542
Ruchira Avatar asked Aug 03 '11 16:08

Ruchira


People also ask

Can you convert PPI to DPI?

ppi↔dpi 1 ppi = 1 dpi.

What is the DPI of Android?

In Android, we have a baseline density of 160 dots-per-inch(dpi).

How do I know my PPI DPI?

If you know the PPI – again, let's say 300 – you divide the total dimensional pixels to determine the inches: 1920/300 = 6.4. 1080/300 = 3.6.


2 Answers

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.

like image 84
Michael Allen Avatar answered Sep 22 '22 22:09

Michael Allen


I don't agree with answer by Michael Allen because the resulting resolutions for ldpi, mdpi, hdpi and xdpi doesn't satisfy the 3:4:6:8 scaling ratios for alternative bitmaps mentioned in the google docs here under 'Alternative Drawables'

http://developer.android.com/guide/practices/screens_support.html#testing

Therefore I would suggest that you take the baseline example that has a minimum size of

470 x 320 dp now using formula from same documentation we calculate full screen resolution for baseline screen size

px = dp * (dpi/160); for baseline px = dp * (160/160) = dp * 1 so px = dp. This means the full screen size for our baseline config. in pixels would be

470 X 320 px (mdpi)

now to follow the 3:4:6:8 scaling ratios for alternative drawables sizes for ldpi, hdpi and xhdpi we need to derive the unit values from mdpi. i.e.

470/4 = 117.5

320/4 = 80

divide by 4 because scaling ratio for mdpi is 4, scaling ratios for ldpi, hdpi and xhdpi are 3,6 and 8 respectively. now just multiply the unit results 117.5 and 80 with these scaling factors

ldpi

117.5 * 3 = 352.5

80 * 3 = 240

mdpi

117.5 * 4 = 470

80 * 4 = 320

hdpi

117.5 * 6 = 705

80 * 6 = 480

xhdpi

117.5 * 8 = 940

80 * 8 = 640

These sizes are now in perfect 3:4:6:8 scaling ratios.

like image 27
Nouman Hanif Avatar answered Sep 21 '22 22:09

Nouman Hanif