Home > Tutorials > Image Processing – Rotate Image On The Fly

Image Processing – Rotate Image On The Fly


Image rotation is quite simple over Android. The trick here is to make use of Matrix.

From my original beauty:

Original Image

Original Image

These are several image rotation on degrees:

Rotation: Degree = 45

Rotation: Degree = 45

Rotation: Degree = 135

Rotation: Degree = 135

Rotation: Degree = 270

Rotation: Degree = 270

Pretty nice effect!

Here the implemetation:

	public static Bitmap rotate(Bitmap src, float degree) {
		// create new matrix
		Matrix matrix = new Matrix();
		// setup rotation degree
		matrix.postRotate(degree);

		// return new bitmap rotated using matrix
		return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
	}

Hope you like it!

Cheers,
Pete Houston

Categories: Tutorials Tags: , , ,
  1. August 16, 2020 at 11:33 pm

    I applied it properly, but the image is not rotating.

  2. shery
    March 31, 2017 at 5:45 pm

    how we can this type of method call in a button ??

  3. June 19, 2013 at 7:50 pm

    This is the most memory inefficient way to do so.
    What you do is run a postRotate() on an existing matrix, and when drawing that bitmap on a canvas, apply this matrix.

    Creating a new bitmap for this sort of thing is pointless and inefficient.

    More information here: http://judepereira.com/blog/multi-touch-in-android-translate-scale-and-rotate/

  4. March 2, 2012 at 1:01 pm

    Thanks for your post, i have a problem that bitmap is rotating fine but bitmap size is chaged for every rotation….

  5. February 11, 2012 at 7:51 pm

    Nice post ..

  6. Gogomx3
    December 27, 2011 at 10:22 pm

    Cool

  1. No trackbacks yet.

Leave a comment