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

Advertisements
Categories: Tutorials Tags: , , ,
  1. shery
    March 31, 2017 at 5:45 pm

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

  2. 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/

  3. 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….

  4. February 11, 2012 at 7:51 pm

    Nice post ..

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

    Cool

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: