Rotating a bitmap using matrix
Asked Answered
T

4

1

While rotating a bitmap using matrix, vertex is not stable..

  Matrix matrix = new Matrix();
    matrix.postRotate(mDegree,100,100);
    mCompasstemp = Bitmap.createBitmap(mCompPic, 0, 0, mCompPic.getWidth(), mCompPic.getHeight(), matrix, true);
    mCompassHud.setImageBitmap(mCompasstemp);

Output of my code is like -bitmap will rotate. -vertex of my bitmap is not stable. -Bitmap is resizing

I need disable image resizing and make the rotation stable.Can you please suggest a solution for this?

Titter answered 9/8, 2010 at 13:56 Comment(0)
P
5

Rather than creating your new Bitmap directly from the original, another (more straight-forward, imho) option is to create the resultant Bitmap, create a Canvas with that Bitmap, then do your rotation/translation/scaling on the Canvas and draw the original Bitmap onto the new Bitmap via the Canvas.

Basically, you're looking, then, at:

  scaledImage = Bitmap.createBitmap (croppedWidth, croppedHeight, Bitmap.Config.ARGB_8888);

  Canvas offscreenCanvas = new Canvas (scaledImage);
  Matrix matrix = new Matrix();
  matrix.setRotate (rotations, centreX, centreY);
  matrix.postScale(scaleX, scaleY);
  offscreenCanvas.setMatrix (matrix);

  offscreenCanvas.drawBitmap (pickedImage, 0, 0, new Paint(Paint.DITHER_FLAG));
Prau answered 25/11, 2010 at 17:31 Comment(0)
B
1

Not sure if this is what your looking for but it might help.

Android uses its built in compatibility features to scale and render a bitmap appropriately for screens with different pixel densities. There are two methods of scaling, pre-scaling and auto-scaling.

It will pre-scale bitmaps' from resources and auto-scales when the bitmap is being drawn internally (which is what your doing be using createBitmap).

Go to http://developer.android.com/guide/practices/screens_support.html and check under: 4.Use density and/or size-specific resources: Pre-scaling and auto-scaling of bitmaps and nine-patches

Bowling answered 21/10, 2010 at 0:48 Comment(0)
R
1

I have tried this code, and the rotate is stable at the center of the bitmap

matrix.reset();
matrix.setRotate(degree, Xpos+bitmap.getWidth()/2, Ypos+bitmap.getHeight()/2);

and then in canvas doDraw()

canvas.setMatrix(matrix);               
canvas.drawBitmap(bitmap, Xpos, Ypos, null);
canvas.setMatrix(null);

The Xpos and Ypos is the X and Y position of the bitmap The setMatrix(null), set the matrix to null, so that the rotate didn't affect the after bitmap And it didn't always create new bitmap, so it's great for performance I hope that help

Radioscopy answered 25/5, 2011 at 0:58 Comment(0)
S
0

I know its an old question but, all answers with code imply a canvas, so heres a solution without a canvas that worked for me :

Matrix matrix = new Matrix();
matrix.postRotate(mDegree,100,100);
mCompasstemp = Bitmap.createBitmap(mCompPic, 0, 0, mCompPic.getWidth(),
    mCompPic.getHeight(), matrix, true);

mCompasstemp = Bitmap.createScaledBitmap(mCompassTemp, mCompPic.getWidth(),
    mCompic.getHeight(), false);

mCompassHud.setImageBitmap(mCompasstemp);

So basically after rotating your bitmap you rescale it to the desired size. Might not be best practice, but at least you dont have to create an extra canvas if you dont want/need to, since the involving Paint() Object is not an inexpensive operation either.

Sharlenesharline answered 19/3, 2015 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.