5

I want to scale images such that they fit optimally the screen size and don't consume more memory than required.

Currently I put images made for a 640 x 960 screen in hdpi folder, it looks good, but it consumes a lot more memory than necessary. A 480 x 800 device, for example, is hdpi and will load these bitmaps.

Since most hdpi devices I know are 480 x 800, my first thought would be to scale down all the images in hdpi to fit a 480 x 800 screen. But I'm not sure, since hdpi doesn't refer to only to resolution, but density.

It's confusing since the folders refer to density, but the bitmaps are just pixels, and I don't know how to assign them there...

Any help is greatly appreciated.

To give more context: The thing is that I have a bitmap animation with more than 20 bitmaps each 640 x 588 px. So, on some devices this alone will run in out of memory. The memory occupied with 640 x 588 is 1.43 mb each bitmap !. If I scale down to 480 x 441 I get 0.80 mb and that's a huge improvement. And since in a 480 x 800 device I'll not lose detail, I would scale all the tiles down.

User
  • 31,811
  • 40
  • 131
  • 232

1 Answers1

6

In my opinion, images sized to fit the full screen are just not a good idea for Android in general. That said, if it's what you need to do, I would worry less about the memory usage and opt for the largest size you think you will need for a particular density, and just let the OS scale down from there for small variances in screen size. Don't forget you can also specify drawables by screen size as well (e.g. drawable-large-hdpi) if you need to get more specific.

Depending on the image, if it's possible to make any part of the background stretchable, I would recommend making it a 9-patch, and sizing it to fit the smallest screen in that density bucket and let it fit to the screen that way.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • I have an animation with more than 20 bitmaps each 640 x 588, that's why every bit I scale down is important. With 480 x 441 bitmaps the consumed memory was reduced like 40% (!) First I thought the actual consumed memory is the bitmap after scaled down, but it loads the whole bitmap in original size and then scales down, so, it will occuppy lots more memory the bigger the bitmaps are – User Jul 23 '12 at 14:34
  • That seems...excessive. Possibly just keep one set of bitmaps in `drawable-nodpi`, and on first launch, convert and cache a local set at a proper resolution for the device it's running on? – Kevin Coppock Jul 23 '12 at 14:36
  • cache... no thanks. I'll be loading also other images and this animation is very seldom, so it will be a waste of memory. I would prefer to do this conversion each time I run the animation. But I guess that like playing normally the animation, it first requires that all the bitmaps are loaded in original size into memory, so I have the same problem. – User Jul 23 '12 at 14:40
  • I mean caching to local storage (like the SD card or SD partition) on the first run of the application, then they can be loaded into memory only when needed. This way you don't have the wasted space of multiple sets of images that may never be accessed. – Kevin Coppock Jul 23 '12 at 14:42
  • Hm... interesting idea. So I put them in hdpi in big size, and once in the device I know exactly the size and put the scaled images in the SD card... sounds good and probably I'll do that, if there's no other solution, when I have more time. But for now I'm still looking for an optimal, minimal bitmap size to put in hdpi. – User Jul 23 '12 at 14:45
  • Well as far as that goes, I don't know offhand of an HDPI device below 480x800 (although in theory a 240x240 screen could be HDPI at 1"x1") so I think 480 wide would suffice in the majority of cases, and if it needs to be upscaled, there wouldn't be a very noticeable loss of quality. – Kevin Coppock Jul 23 '12 at 14:48
  • Yeah, and anyways, the SD card idea wouls be tedious to implement for other things, like button backgrounds, activity backgrounds, etc. Could not use them just in XML... +1 because useful but the answer is not quite complete, at least concerning the question's title... – User Jul 23 '12 at 15:01
  • You certainly wouldn't want to implement that for all of the other drawables. This suggestion was meant only for your specific bitmaps that you wanted to fill the screen. For other drawables, I would refer you to [this answer](http://stackoverflow.com/a/11581786/321697). Quite honestly, I'm not sure what part of the question you still need answered. – Kevin Coppock Jul 23 '12 at 15:07