Android rotates a bitmap around the center without resizing

I'm struggling to draw a rotating bitmap around its center and do the rotation without resizing the bitmap. I draw all my sprites on the screen through the game stream, so I'm looking for a solution that includes the original bitmap and not the canvas.

Thanks in advance.

This is my code so far, it turns a bitmap around its center, but resizes it.

i = i + 2;
            transform.postRotate(i, Assets.scoresScreen_LevelStar.getWidth()/2, Assets.scoresScreen_LevelStar.getHeight()/2);
            Bitmap resizedBitmap = Bitmap.createBitmap(Assets.scoresScreen_LevelStar, 0, 0, Assets.scoresScreen_LevelStar.getWidth(), Assets.scoresScreen_LevelStar.getHeight(), transform, true);

            game.getGraphics().getCanvasGameScreen().drawBitmap(resizedBitmap, null, this.levelStar.getHolderPolygons().get(0), null);

Update:

I noticed that it is not as simple as it seems. My spinning code is not a problem. The bitmap rotates, but dst rect should also increase / decrease depending on the rotation angle, otherwise the bi-map will be smaller, because it is pulled into a fixed dst rect. Therefore, I assume that I will have to develop some method that will return dst rect. Therefore, the methods required to rotate a bitmap image have not changed:

public static Bitmap rotateBitmap(Bitmap bitmap, int rotation) // I've got this method working

and

public static Rect rotateRect(Rect currentDst, int rotation) // Don't got this

I understand that this will require some kind of math (trigger), someone to call ?: P

+5
source share
2 answers

It worked for me!

I created a method that returns a matrix. The matrix can be used in the following drawing method:

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

! ( , , ):

public static Matrix rotateMatrix(Bitmap bitmap, Shape shape, int rotation) {

        float scaleWidth = ((float) shape.getWidth()) / bitmap.getWidth();
        float scaleHeight = ((float) shape.getHeight()) / bitmap.getHeight();

        Matrix rotateMatrix = new Matrix();
        rotateMatrix.postScale(scaleWidth, scaleHeight);
        rotateMatrix.postRotate(rotation, shape.getWidth()/2, shape.getHeight()/2);
        rotateMatrix.postTranslate(shape.getX(), shape.getY());


        return rotateMatrix;

    }

. , , . 1, 2, 3...

0

, Matrix. , , "". . onDraw() , . .

public class Ship extends View {

    private float x, y;
    private int rotation;
    private Matrix position;    
    private Bitmap bitmap;

    ...

    @Override
    public void onDraw(Canvas canvas) {
        // Draw the Bitmap using the current position
        canvas.drawBitmap(bitmap, position, null);
    }

    public void update() {
        // Generate a new matrix based off of the current rotation and x and y coordinates.
        Matrix m = new Matrix();
        m.postRotate(rotation, bitmap.getWidth()/2, bitmap.getHeight()/2);
        m.postTranslate(x, y);

        // Set the current position to the updated rotation
        position.set(m);

        rotation += 2;
    }

    ....

}

, !

, Bitmap .

+7

All Articles