3

When we use images in Android layouts, the height and width of the ImageView is usually set in dp which is same all screen resolution. In the early days of Android development, resolutions were getting higher and lower resolution images were shown magnified which does not look good. But now the resolutions are mostly stable. And if we use only highest resolution images, it will be scaled down in lower resolution screens and will still look good. Then why don't we use only the highest resolution images instead of providing 4-5 resolution versions for each image? Is there any technical upside?

Gulshan
  • 3,611
  • 5
  • 35
  • 46
  • 2
    If you will use high resolution images on [low end devices](https://www.phonearena.com/news/What-makes-a-high-end-phone-different-from-a-low-end-one_id77646) then it will give you out of memory crashes, thats why google have provided different resolutions and dpi for all device ranges. – Maveňツ Mar 01 '18 at 07:57
  • sure, the main reason is that low-en devices will go OutOfMemory if you load too large images – Vladyslav Matviienko Mar 01 '18 at 08:03
  • 1
    Possible duplicate of [Only use XHDPI drawables in Android app?](https://stackoverflow.com/questions/5998865/only-use-xhdpi-drawables-in-android-app) You will find you answer here most probably. However, I have to disagree with the above comments: if scaling is done properly, you will never have a OOM. The main reason for providing multiple versions is because scaling an image is computationally expensive and you do not want to add more work for a system that's running on low specs. – Iulian Popescu Mar 01 '18 at 08:28

1 Answers1

3

There’s nothing stopping you using only high resolution images - but there are a few gotchas.

Imagine you have a single high quality image in your resources that is to be displayed on the screen. You check the Android Dashboard and determine the majority of the screens are normal, with xhdpi density. Does this give you enough information on how large the image in resources should be, in order to look good on every single screen in the wild?

OK, better safe than sorry you murmur to yourself, while deciding to leave the poster-size image in your resources. The image is, say, 70MB. Your APK size increases dramatically, but who cares, right?

Next, you write some code to display the image. You like to test on old, low end devices. If it works on those, it should work on your brand spanking new Samsung S9. You run the app and are greeted with OutOfMemoryError. Guess some devices can’t handle such large images, go figure.

So you do some googling, and stumble upon Loading Large Bitmaps Efficiently. You apply the subsampling, and voila, the image loads. The only problem now is figuring out what the image is actually showing, as the compression completely skewed its original quality.

So you go back to the original image, do some magic, and produce a smaller, but still viable image with, say, 10MP. The loading might still fail on older devices, so you leave the subsampling in, but the compression is now much less severe. Done & dusted!

A few days later, a QA report says the image is incomprehensible on an Android TV. Guess it’s back to the drawing board…

Android fragmentation is a real deal. Here’s an excerpt from Screen Compatibility Overview:

Android runs on a variety of devices that have different screen sizes and pixel densities. The system performs basic scaling and resizing to adapt your user interface to different screens, but there is more work you should do to ensure your UI gracefully adapts for each type of screen.

while things are (arguably) getting better, following good practices and advice from the docs is still the safest way to go.

Tadej
  • 2,901
  • 1
  • 17
  • 23
  • Just to clarify, by highest quality/resolution image, I meant images for xxhdpi resolutions. As that image version is already going to the apk along with some other versions for other screen resolutions, apk size was not my concern in this question. – Gulshan Mar 01 '18 at 11:09