Jens Zalzala
Jens Zalzala

Reputation: 2576

Trying to display a large image as tiles without hiccups

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!

Upvotes: 0

Views: 2365

Answers (4)

M. Usman Khan
M. Usman Khan

Reputation: 4458

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).

Upvotes: 0

Jens Zalzala
Jens Zalzala

Reputation: 2576

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.

Upvotes: 1

michaelg
michaelg

Reputation: 2692

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.

Upvotes: 0

dave.c
dave.c

Reputation: 10908

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?

Upvotes: 0

Related Questions