The fastest way to rotate a large bitmap

I work with large images, and when I try to rotate them (applying matrix on the bitmap) , it takes many seconds. I have seen that the Android system gallery can accomplish this task very quickly. How is this possible?

I thought you were performing a rotation on asyncTask, applying only the ImageView rotation (which does not take much time) in the main thread, but if the application is killed before asyncTask terminates, the application will end up in an inconsistent state.

This is my bitmap rotation code, which takes a lot of time for large bitmaps:

 Matrix mat = new Matrix(); mat.setRotate(90); bMap = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), bMap.getHeight(), mat, true); 
+5
source share
1 answer

When making changes to a large image, create a low-quality bitmap copy of it and use it to display instant rotation or other editing effects. Once the user is stopped, moving (or left-clicking the slider) replace the low-quality bitmap with the original image with the same effects that apply to it. This will greatly improve the user experience. Give it a try and let me know.

This may seem like a deviation from the original requirement - you can draw a rectangle instead of rotating the entire image in real time. In the end, what the user needs is an indication of how much his image has rotated. It will be faster and can be easily done in the user interface thread without delay.

This is the simplest rotation on canvas.

 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. 
+3
source

Source: https://habr.com/ru/post/925924/


All Articles