Skip to content Skip to sidebar Skip to footer

Rotate A Sprite In Game Android

I want implement move a sprite from position (x ,y ) to position action_down (x1 , y1) .But I can't rotate it .Please help me .Thanks This is my code: public Sprite(GameView gameVi

Solution 1:

You should look at matrix.postRotate or canvas.rotate.

Solution 2:

Here you go: Note: you need to convert from Bitmap to Image.

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

/**
 * Created by Chris on 3/28/2014.
 */publicclassSprite {
    private Image i;

    publicSprite(Image image) {
        this.i = image;
    }

    privateBufferedImageimage=null;
    privateGraphics2Dgraphics=null;

    publicvoidonDraw(Canvas canvas) {
        if(image == null || graphics == null) {
            setup();
        }
        Graphicsg= canvas.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
        //Where to draw the Sprite on the canvas.intx=100;
        inty=100;
        //Because graphics is an instance of Graphics2D//Converts the degrees "45" to radians.doublerotationAngle= Math.toRadians(45);
        doublelocX= image.getWidth() / 2;
        doublelocY= image.getHeight() / 2;
        AffineTransformtx= AffineTransform.getRotateInstance(rotationAngle, locX, locY);
        AffineTransformOpop=newAffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);

        graphics.drawImage(op.filter(image, null), 0, 0, null);

        g.drawImage(image, x, y, (int) (image.getWidth() / 2), (int) (image.getHeight() / 2), null);
    }

    /**
     * Sets the Image up.
     */privatevoidsetup() {
        if(image != null) {
            image.flush();
            image = null;
        }
        if(graphics != null) {
            graphics.dispose();
            graphics = null;
        }
        image = newBufferedImage(i.getWidth(null) * 2, i.getHeight(null) * 2, BufferedImage.TYPE_INT_ARGB);
        graphics = image.createGraphics();
    }
}

Post a Comment for "Rotate A Sprite In Game Android"