0

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 gameView, Bitmap bmp) {

      this.gameView = gameView;
      this.bmp = bmp;
      this.width = bmp.getWidth() / BMP_COLUMNS;// create width, height
      this.height = bmp.getHeight() / BMP_ROWS;
      Random rnd = new Random(System.currentTimeMillis());
      x = rnd.nextInt(gameView.getWidth() - bmp.getWidth());
      y = rnd.nextInt(gameView.getHeight() - bmp.getHeight());
}

public void onDraw(Canvas canvas) {

            Matrix matrix = new Matrix();
            matrix.postTranslate(x, y);
            float dx = x1-x;
    float dy = y1-y;
    float d = (float)Math.sqrt(dx*dx+dy*dy);
    vx = (float) (dx*5/d)/3 ;
    vy = (float) (dy*5/d)/3 ;
    if(k==1){
    x += vx ;
    y += vy ;
    }
    currentFrame = ++currentFrame % BMP_COLUMNS;
    int srcX = currentFrame * width;
    int srcY = 0 * height;
    Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
    Rect dst = new Rect(x, y, x + width, y + height);
    canvas.drawBitmap(bmp, src, dst, null);
}
alex
  • 479,566
  • 201
  • 878
  • 984
Nguyen Nguyen
  • 47
  • 2
  • 9

2 Answers2

0

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

Ben Ruijl
  • 4,973
  • 3
  • 31
  • 44
  • I implement matrix.postRotate() but in command canvas.drawBitmap(bmp, src, dst, null); I can't write canvas.drawBitmap(bmp, matrix , null); because it's don't create a sprite . – Nguyen Nguyen May 06 '12 at 08:27
  • If I understand correctly, your problem is that you want to draw a rotated subbitmap. You can try my second suggestion, namely `canvas.rotate`. I have found an example here: http://stackoverflow.com/a/8676603/314784 – Ben Ruijl May 06 '12 at 10:09
0

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.
 */
public class Sprite {
    private Image i;

    public Sprite(Image image) {
        this.i = image;
    }

    private BufferedImage image = null;
    private Graphics2D graphics = null;

    public void onDraw(Canvas canvas) {
        if(image == null || graphics == null) {
            setup();
        }
        Graphics g = canvas.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
        //Where to draw the Sprite on the canvas.
        int x = 100;
        int y = 100;
        //Because graphics is an instance of Graphics2D
        //Converts the degrees "45" to radians.
        double rotationAngle = Math.toRadians(45);
        double locX = image.getWidth() / 2;
        double locY = image.getHeight() / 2;
        AffineTransform tx = AffineTransform.getRotateInstance(rotationAngle, locX, locY);
        AffineTransformOp op = new AffineTransformOp(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.
     */
    private void setup() {
        if(image != null) {
            image.flush();
            image = null;
        }
        if(graphics != null) {
            graphics.dispose();
            graphics = null;
        }
        image = new BufferedImage(i.getWidth(null) * 2, i.getHeight(null) * 2, BufferedImage.TYPE_INT_ARGB);
        graphics = image.createGraphics();
    }
}
Czipperz
  • 3,268
  • 2
  • 18
  • 25
  • You're posting desktop java code. This doesn't apply to android Canvas graphics work. – MacD May 09 '14 at 22:32