Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to rotate big size bitmap

Tags:

android

bitmap

I am working with large size images and when I try to rotate them (applying matrix on the bitmap) a lot of seconds occurs. I've seen that android system gallery can accomplish this task in a very fast manner instead. How is it possible?

I thought to perform the rotation on an asyncTask, applying only the ImageView rotation (which doesn't take long time) on the main thread, but if the app is killed before the asyncTask get to the end, the app fall in an inconsistent state.

This is my bitmap rotation code, which takes long time execution for large bitmaps:

Matrix mat = new Matrix();
mat.setRotate(90);
bMap = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), bMap.getHeight(), mat, true);
like image 833
Joe Aspara Avatar asked May 19 '12 09:05

Joe Aspara


1 Answers

When doing modifications to a large image, create a low quality bitmap copy of it and use it for showing instant rotation or other editing effects. Once user is stopped motion (or left slider control) replace the low quality bitmap with the original image with same effects applied to it. This will significantly improve user experience. Try and let me know.

This might sound like a deviation from the original requirement- you can draw a rectangle instead of rotating the whole image in real time. In the end, what user needs is an indication of how much his image has rotated. This will be faster and can easily be done on UI thread without lag.

This is the simplest rotation on canvas code

canvas.save(); //save the position of the canvas
canvas.rotate(angle, X + (imageW / 2), Y + (imageH / 2)); //rotate the canvas
canvas.drawBitmap(imageBmp, X, Y, null); //draw the image on the rotated canvas
canvas.restore();  // restore the canvas position.
like image 95
Ronnie Avatar answered Oct 19 '22 10:10

Ronnie