Giammy
Giammy

Reputation: 83

Android loading of large bitmaps

I'm making an Android app and I need to load an image (bitmap) in a cavas and resize it using the "pinch zoom" gesture. When the image is over a certain size, however, the application crashes (OutOfMemory exception). How do I optimize the loading and manipulation of the image?

To load the image I use:

BitmapFactory.decodeResource (ctx.getResources (), R.drawable.image)

To draw it:

imgCanvas.drawBitmap (image, posX, posY, null), 

To change its size:

Bitmap.createScaledBitmap (originalBitmap, neww, NEWH, true);

Upvotes: 8

Views: 1140

Answers (1)

Streets Of Boston
Streets Of Boston

Reputation: 12596

This is not trivial.

Based on the current scale of the image and the currently visible part of the image, only load a part of that image at the appropriate resolution:
https://developer.android.com/reference/android/graphics/BitmapRegionDecoder.html

When zoomed-out and you want to show the entire image scaled down, use the methods from this BitmapRegionDecoder class that take a BitmapFactory.Options parameter and set it inSampleSize to a value larger than 1 (preferably a value that is a power of 2):
https://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize

When zooming in, first zoom in the lower resolution that is already shown (where you used a value of inSampleSize > 1) and lazily load a higher resolution version (where inSampleSize is smaller than the previous value you used) using the BitmapRegionDecoder and fade in the higher resolution version gradually.

When the user zooms in, keep doing this until your inSampleSize is 1.

Upvotes: 4

Related Questions