Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm struggling to draw a rotating bitmap around its center and do the rotating without resizing the bitmap. I'm drawing all my sprites to the screen via a game thread, so I'm looking for a solution that incorporates the original bitmap and not the canvas.

Thanks in advance.

This is my code so far, it turns a bitmap around its center, yet 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've noticed this isn't as easy as it sounds. My rotating code is not the problem. The bitmap rotates, yet the dst rect will also have to increase/decrease depending on the angle of rotation, or else the bimap will appear smaller, since it's drawn into a fixed dst rect. So I'm guessing I'll have to develop some method that will return a dst rect. So the methods needed to rotate a bitmap without appeared resizing:

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 this will require some math (trig), anyone up for the challenge? :P

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
560 views
Welcome To Ask or Share your Answers For Others

1 Answer

You should draw the bitmap using the Matrix class. Below is a very basic idea assuming that you want to rotate an image inside of a "Ship" class. You update the current position Matrix inside the update method. In the onDraw() you draw the bitmap using the newly updated position Matrix. This will draw the rotated bitmap without resizing it.

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;
    }

    ....

}

Hope that helps!

Also keep in mind that generating a new Bitmap object inside you game loop will be resource intensive.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...