0

My tablet app has to display a very large image (2500x6000) and allow the user to pan across, zoom in & out. Since it can't fit into memory I've broken it into tiles and am loading those as needed. The problem I'm running into is that whenever I unload or replace a bitmap I cause garbage collection which pauses my app with noticeable stutter. I was wondering if anyone had come up with any clever ways to work around this? I create my bitmaps using BitmapFactory.decodeResource. I've tried replacing the same bitmap but garbage collection still runs (assuming it dumps the old bitmap and replaces it with a new one).

Thanks!

Jens Zalzala
  • 2,546
  • 1
  • 22
  • 28

4 Answers4

1

Figured out the answer! In API11+ BitmapFactory.Options has an attribute called inBitmap which will reuse the bitmap when loading content. I've implemented it as such:

mBg[i] = Bitmap.createBitmap(800, 1232, Bitmap.Config.RGB_565);
mBgOptions[i] = new BitmapFactory.Options();
mBgOptions[i].inBitmap = mBg[i];
mBgOptions[i].inPreferredConfig = Bitmap.Config.RGB_565;
mBgOptions[i].inMutable = true;
mBgOptions[i].inSampleSize = 1;

The garbage collector no longer runs and the pauses have been removed. As an f.y.i, inSampleSize has to be set or it won't work.

Jens Zalzala
  • 2,546
  • 1
  • 22
  • 28
0

I used this easy to integerate source of WorldMap application:

https://github.com/johnnylambada/WorldMap

This uses a huge image of a world map, and uses cache to display a map.

To integerate, I just copied all the java files (5 i guess) and used the surfaceView in my layout file. Then I went through the small OnCreate() method of ImageViewerActivity.java and used the code in my activity (with sligh alteration, depending on my personal use).

M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69
0

If you are targeting Android 3.0 then this answer may help:

Pre-Android 3.0 you could build an HTML page of your tiled images and let the built-in browser handle the load and unload of the images. My answer here has some more details:

Anyone else have any alternative approaches?

Community
  • 1
  • 1
dave.c
  • 10,910
  • 5
  • 39
  • 62
  • Loading the tiles isn't really the issue... except for the garbage collection bit, and I don't think the other approach will change that. I may try the browser approach but I don't think a web view will really work for the behavior we're trying to get. – Jens Zalzala Feb 06 '12 at 18:06
0

Are you using android:largeHeap="true"? That might reduce the frequency of GCs. Also given you are targeting tablets, then you can safely assume that you are dealing with a concurrent garbage collector. So it will work best if it has more, smaller chunks of memory to collect, i.e. smaller tiles.

michaelg
  • 2,692
  • 1
  • 17
  • 16
  • Thanks for the info. I'm not using android:largeHeap="true"... I'll have to look into that. I'll also try using smaller chunks. – Jens Zalzala Feb 06 '12 at 19:41