views:

190

answers:

1

Im currently developing a program that uses a scrollable/zoomable image as the main user interface. It uses a canvas which is manipulated by a matrix to traverse a large area. Instead of loading a super large bitmap into memory, i wanted to employ a tile system to load 256 by 256 squares. My problem is that the app will lag when the images are being loaded into memory. I use a simple outer and inner for loop to load the tiles if they are null, and if the user zooms out to a certain extent the tiles all disappear and a lower res version of the whole image is shown. Ive been steered into the direction of asynchronous image loading, which seems like it would prevent the lag while the image loads into memory but i have no idea how to start this, and was wondering if anyone had any advice on how to generally asynchronously load bitmaps into the canvas. Thanks!

+1  A: 

I simply use a second thread to do ANYTHING that could take some time to make sure that the UI is always nice and responsive. You may even want to lower the priority of that thread if the UI contains animation (like scrolling) to get rid of stuttering.

You could use a second Thread object that sends a message via a Handler when it's done, or you can use AsyncTask.

EboMike
I will look into both of those, thanks. Once i have async task loading my bitmaps into memory and loading them in my UI once they are done what would be the most effective way to recycle the bitmaps to avoid an out of memory error? another async task that checks for bitmaps no longer being viewed?
Evan Kimia
Bitmap.recycle() to speed-kill them. Other than that, the number of most important thing is to make sure you don't hold any more references to them. Be extremely careful with static members, and be sure to remove them from any arrays/lists you have - including arrays of views pointing to drawables pointing to those bitmaps :)
EboMike
awsome, thank you so much. to use bitmap.recycle(), would the best way to detect if the bitmap is no longer being viewed be to have an array that records which bitmaps are being drawn, then have a for loop that runs through every bitmap to see if doesnt equal those values? Its what i have now, and it seems like there should be a simpler way to determine what bitmaps are drawn on screen.
Evan Kimia
Depends - who's managing your Views? If it's in something like a Gallery, I would just let the View take care of it for you: Make sure you don't hold any references to any of the Views, and make sure nobody has a reference to the bitmap other than the view itself. With that, once the view is abandoned by the OS, the bitmap will go too if memory is low. That's the general answer - anything other than that would require knowledge on exactly how your system is set up.
EboMike
thank you. I actually just have one custom view with the bitmaps drawn on the campus. I will first get async task to correctly load my bitmaps then tackle any memory issues. thanks for everything.
Evan Kimia
Here is part 2 of my question.http://stackoverflow.com/questions/3401919/optimizing-a-bitmap-loading-thread-being-called-by-ondraw
Evan Kimia