Android - Adding Images To Project
Asked Answered
C

2

18

I am brand new to Android and I am using Eclipse ADT to create a simple application. This application has a button that changes it's background image based on user clicks.

I noticed that there are 4 folders for images in my project: drawable-hdpi, drawable-ldpi, drawable-mdpi, and drawable-xhdpi.

and I also noticed that the images that are in there currently (the launcher icon) are all different sizes.

Does this mean that I have to use some image editing software to create one image for each resolution for each of my images? Or (hopefully) is there a way to import an image and have this done for me automatically?

Thanks!

Calculable answered 25/2, 2013 at 1:47 Comment(0)
G
44

Images and other visual files are stored in one or more drawable directories. If only in one directory, Android will scale the image as needed. If more than one directory is used, Android will select the appropriately sized image.

  • drawable-ldpi - Low density images
  • drawable-mdpi - Medium density images
  • drawable-hdpi - High density images
  • drawable-xhdpi - Extra high density images (i.e. retina-like displays)
  • drawable-xxhdpi - Extra extra high density images (devices like Nexus 10, Samsung Galaxy S4, HTC One and Sony Xperia Z)
  • drawable-xxxhdpi - Triple extra high density images (Nexus 6 and 9)

(Side note: XML files can also be written and stored as drawables. These kinds of files can control when multiple images are to be used based on the state of a view, or other visual settings like gradients, borders, etc.)

So, what should you do?

For best results (from the Android developer docs):

To generate these images, you should start with your raw resource in vector format and generate the images for each density using the following size scale:

  • xxxhdpi: 4.0
  • xxhdpi: 3.0
  • xhdpi: 2.0
  • hdpi: 1.5
  • tvdpi: 1.33 (TVs only)
  • mdpi: 1.0 (baseline)
  • ldpi: 0.75

This means that if you generate a 200x200 image for xhdpi devices, you should generate the same resource in 150x150 for hdpi, 133x133 for tvdpi, 100x100 for mdpi, and finally a 75x75 image for ldpi devices.

If you just want to use one image and let Android scale for you:

More than 75% of Android devices now have hdpi or greater resolutions, according to the Dashboard on the Android Developer site. So if you create one image at hdpi, for example, it will look perfect on about a third of devices, scale up for some, and scale down for about 25% of devices. In general I think you'd be better off scaling down than scaling up, too.

Guillemot answered 25/2, 2013 at 1:52 Comment(4)
Thanks! I researched this very issue recently.Guillemot
I have 20 images. Each of them are 100x100px. I guess that means I have to create 60 more images, one each for the other 3 densities. Can you recommend a program to simplify this task? I use paint.net but it doesn't do batch processing.Calculable
I don't know what's best, but here are some options. I've used the "mogrify" command line tool from ImageMagick: imagemagick.org/script/mogrify.php. Here's a SuperUser forum post that might help, too: superuser.com/questions/315885/fast-batch-image-resizerGuillemot
Good answer Ben, you should add tvdpi to your listForetoken
C
3

Does this mean that I have to use some image editing software to create one image for each resolution for each of my images? Or (hopefully) is there a way to import an image and have this done for me automatically?

If you already have high resolution sources (or vector graphic formats) of your images, you can use the Android Asset Studio to generate icons/images from your own source images for every density (minus xxhdpi). You can also create icons/images from default Android clipart, that can be themed for various versions of Android. This will save you some of the hassle of resizing and saving with an image editing tool.

Clarinda answered 25/2, 2013 at 2:3 Comment(1)
The tool will append ".9" in the images that it creates (ie. image.9.png). When calling the drawable, simply use "image" (skip both ".9" and the extension). Lost a bit of time trying to figure this out.Torre

© 2022 - 2024 — McMap. All rights reserved.