1

How to rotate Bitmap 45 degrees?

Matrix matrix = new Matrix();
matrix.postRotate(degrees);
mBitmapFirst = Bitmap.createBitmap(mBitmapFirst, 0, 0, mBitmapFirst.getWidth(), mBitmapFirst.getHeight());
mImageFirstView.setImageBitmap(mBitmapFirst);

I used this method, but it working only if rotation 90 degrees

Sivolotskiy
  • 77
  • 1
  • 9

2 Answers2

2

you can rotate to 45 degree with this example :

Matrix minMatrix = new Matrix();
//height and width are set earlier.
Bitmap minBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas minCanvas = new Canvas(minBitmap);

int minwidth = bitmapMin.getWidth();  
int minheight = bitmapMin.getHeight();
int centrex = minwidth/2;
int centrey = minheight/2;

minMatrix.setRotate(mindegrees, centrex, centrey);
Bitmap newmin = Bitmap.createBitmap(minBitmap, 0, 0, (int) minwidth, (int) minheight, minMatrix, true);

minCanvas.drawBitmap(newmin, (centrex - newmin.getWidth()/2), (centrey - newmin.getHeight()/2), null);
minCanvas.setBitmap(minBitmap);

where mindegree is your rotation degree,

hope i helps you

Paul
  • 179
  • 1
  • 8
1

Here is the link where use has also achieved memory saving by saving the need for creating new bitmap every time

Community
  • 1
  • 1
Chintan Desai
  • 2,607
  • 2
  • 22
  • 25