Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

How do I convert ppi into dpi for Android images?

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...