Click here to Skip to main content
15,888,968 members
Articles / Mobile Apps / Android
Tip/Trick

Rotate Bitmap

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
27 Aug 2013CPOL 21.2K   561   5   2
Rotate bitmap

Introduction

Today, I will guide a snippet code to create a new bitmap by rotating an original bitmap by using Canvas.

Besides that, if you want to cut bitmap, please refer to this tip. And if you want to combine many bitmaps into one, please refer to this tip.

Using the Code

To rotate bitmap by using canvas, focus on the function below:

C#
public void drawBitmap (Bitmap bitmap, Matrix matrix, Paint paint) 

The main point to rotate is matrix, with method:

Java
matrix.postRotate(angle, x, y);  
  • angle: the angle will be rotated
  • x,y: the coordinate of anchor point to rotate on

With the above explanation, to rotate bitmap with anchor point as center, anchor point will be (bitmap.width/2, bitmap.height/2).

Java
private Bitmap rotateBitmap(float angle) {
 Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
        R.drawable.original);
    Bitmap rotateBitmap = Bitmap.createBitmap(bitmap.getWidth(),
        bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(rotateBitmap);
    Matrix matrix = new Matrix();
    matrix.postRotate(angle, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
    canvas.drawBitmap(bitmap, matrix, null);
    return rotateBitmap;
}  

And rotate bitmap with anchor point is (left, top), anchor point will be (0, 0)

Java
private Bitmap rotate(Bitmap bitmap, float angle) {
    Bitmap rotateBitmap = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(rotateBitmap);
    Matrix matrix = new Matrix();
    matrix.postRotate(angle, 0, 0);
    canvas.drawBitmap(bitmap, matrix, new Paint());
    return rotateBitmap;
}

History

  • 2013-08-27: Created
  • 2013-08-27: Updated to clear more about the anchor point, following the comment from users
  • 2013-08-28: Changed the link name to download source code

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Vietnam Vietnam
I'm Java developer.
I started Java web coding from 2007 to 2010. After that, I began to develop Android from 2010 to now.
In my thinking, sharing and connecting is the best way to develop.
I would like to study more via yours comments and would like to have relationship with more developers in the world.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Andre Sanches (alvs)27-Aug-13 3:00
Andre Sanches (alvs)27-Aug-13 3:00 
GeneralRe: My vote of 1 Pin
huyletran27-Aug-13 3:37
huyletran27-Aug-13 3:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.