49

If you're planning to support LDPI, MDPI, HPDI, and perhaps XHDPI in the near future, is it ok to only include XHDPI drawables in the project and let the devices scale them to their desired resolution?

I've tested to resize the drawables into MDPI and HDPI in Photoshop and then compared the result with XHDPI drawables only resized by Android, and I can't see any difference at all. Is it bad design to take this shortcut? It would be nice to not have to resize each and every drawable into 3 different resolutions.

Planning to use target SDK is 2.1 or 2.2.

BR Emil

1
  • 2
    Bitmap scaling final assets will look far worse than assets draw at size in Photoshop using Shape Layers / Layer Styles etc. Far worse. You're better off creating images at the correct sizes from the source Photoshop document, provided the PSD is build well. Nov 5, 2012 at 4:44

7 Answers 7

25

I guess that's a good way to go. The only downside I can think of is the resource overhead on small scale devices and possible artifacts because of the downscaling. Actually at this year's Google IO Chris Pruett recommended embedding only high resolution assets and let opengl handle the scaling.

5
  • Good to know this. I'm now developing just by using xhdpi drawables. Is there some possibility to improve quality of downscaled images? Some good interpolation method could be great. Sep 6, 2011 at 16:02
  • You can enable mipmapping to have the system create different sized versions of your textures while selecting the best based on viewing distance. This increases rendering speed and decreases artifacts. For more info, refer to en.wikipedia.org/wiki/Mipmap
    – Will Kru
    Aug 15, 2012 at 11:35
  • 12
    The quote about Chris Pruett is a little misleading as it was about games where you'd let the GPU do the downscale.
    – Warpzit
    Oct 23, 2013 at 11:53
  • Link to Chris Pruett's speech please :)
    – fikr4n
    Apr 17, 2014 at 3:46
  • Yes the talk is about using opengl and not the default android components. But essentially mipmaping and different resolution of images for components are solving the same problem. You can precalculate different sized bitmaps or let the system do that on runtime (ONCE and usually cash the results). Things to worry about are the scaling artifacts and the talk was about how opengl will downscale almost always correctly but then again you can have finer downscaling algorithms when they have few seconds to run like in photoshop insted of few tens of ms. May 1, 2014 at 6:36
15

As of Android 1.6, different densities are handled, including XHDPI (which wasn't officially added until 2.2). Your app will first look for an image matching its density, but it can look into larger "buckets" like XHDPI and then perform the scaling for you.

It's best to include specific assets for the densities you want to support. An image that's 100x100 takes 40kb; and image that's 200x200 takes 160k (uncompressed). So, any XHDPI assets used on MDPI devices have four times the amount of data that you need, which has to be handled when the app starts and your resources are prepared. Lower memory use means greater efficiency, less chance for an OutOfMemoryException.

Further, specific types of images will look bad when automatically scaled. In particular, images with fine lines or fine patterns will have their detail muddied. When you shrink the images by hand, you can choose the algorithm that best matches your needs (linear, bicubic, lanczos, etc.).

If you're worried about the time it takes to do the resizing yourself, you can incorporate a batch process or use tools such as Nine Patch Resizer: http://code.google.com/p/9patch-resizer/

4
  • The following statement is untrue: "Android 2.1 did not know about XHDPI resources, so it will crash when your code attempts to use an XHDPI-only asset on 2.1 and older versions." I have never experienced any crashing due to lack of non-xhdpi graphics. Feb 28, 2013 at 15:51
  • Looks like 1.6 and above will handle XHDPI assets; I'll update the answer. Mar 1, 2013 at 0:05
  • I always just assumed it worked due to building the app against a higher SDK level, and that the logic for selecting graphics occurs in the supplied logic. Is there any formal documentation on this? Mar 1, 2013 at 8:00
  • Is there anything new on this topic. Because recently I tried my app with only XHDPI folders and it crashed on Samsung S4 - XXHDPI device. So can I rather use only xxhdpi folders instead of just xhdpi. (I wanna know the technical possibility, I know about the image losing it crispness) Aug 24, 2015 at 8:43
4

I tested in a simple app (develop for Android 2.1) using only xhdpi images and it works fine in small, medium and high resolutions... even I tested in an Android 2.1 (small resolution) and it opens the imagen without problem.

Maybe the thing with the memory is true, so its necessary someone test this.

3

I personally found that using just xhdpi folder works quite good in many apps and am a big supporter of this approach. In memory overhead is true but with todays devices I would consider it negligible. Also I think there is some caching involved after downscaling since I never noticed any slow down because of this. Including only one folder can dramatically reduce your APK size which end users will quite appreciate. You should keep in mind that some images will get scaling artifacts (fine patterns and stuff) but I personally never encountered nothing critical in my apps. Also for buttons and stuff be sure to use 9patches in order to reduce artifacts on rounded corners, you can even slightly reduce image size by using this approach. API level will not be a problem on older versions since I think that drawable-xhdpi is regarded as just drawable on versions that didn't support it. Don't ignore chances to define some simple drawables in xml, for example it's really easy to create gradient background with just shapes and with this you save space and don't risk scaling artifacts.

1
  • 4
    Notice to down-voters, fell free to vote if you think this answer is incorect but please add some explanations why you think so. This is becouse this answer is based on personal opinion and this way I solved the problem worked perfectly every time. Also there are other developers using this approach and I think they would also like to know why this might be a bad practise if you think so. May 1, 2014 at 6:25
2

XHDPI was only introduced in Android SDK API Level 9 (Gingerbread) (see http://developer.android.com/reference/android/util/DisplayMetrics.html#DENSITY_XHIGH) so if you plan to have a minimum SDK level of less than 9 you will also need to provide, at least, HDPI drawables as well otherwise devices with Froyo or below will display nothing.

Update: It actually seems like versions prior to Gingerbread will display xhdpi images: https://groups.google.com/d/msg/android-developers/yjYO91OmoJ4/v3he0ZbKo-UJ

2

It's OK to have only xhdpi resources. But note that xhdpi was introduced with api level 9 (gingerbread). That is, if you target api levels <=8 you need at least also hdpi resources.

0

This statement about extra memory usage is wrong.

If you put XHDPI sized drawables inside MDPI folder, then you will have memory problems.

But if you provide XHDPI drawables inside XHDPI folder, then no extra memory will be used since android downsamples the images by skipping parts of it.

This skipping is the reason why you need to provide drawables for every density you plan to support in order them to look good.

On the other hand, only certain images will look bad when downsampled (mostly small icons) so as long as the image has enough data to be thrown away, it will look ok. Or imagne if you have a grid as a drawable, so potentialy some grid lines can get thrown away and the image will look bad.

In the end, it is better for you to experiment with different devices, then you can tell which drawales need alternative resources for their density.

2
  • 2
    So Android is actually clever enough to not read the entire image into memory before performing the downsampling? If it just skips pixels, it just does nearest-neighbour sampling? Citation needed.
    – Karu
    Apr 29, 2014 at 4:25
  • I too find this strange that they would just skip the pixels, I think they use some other algorithm but might be wrong, some source would be great. Still you will probably not need to worry about memory becouse bitmap data will be managed in native memory not on java heap and even on low end devices you have more than enough memory even for large images. May 1, 2014 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.