Rotation of bitmap using a frame by frame animation
Posted
by
pengume
on Game Development
See other posts from Game Development
or by pengume
Published on 2011-03-08T10:49:07Z
Indexed on
2011/03/08
16:19 UTC
Read the original article
Hit count: 417
Hey every one I know this has probably been asked a ton of times but I just wanted to clarify if I am approaching this correctly, since I ran into some problems rotating a bitmap. So basically I have one large bitmap that has four frames drawn on it and I only draw one at a time by looping through the bitmap by increments to animate walking. I can get the bitmap to rotate correctly when it is not moving but once the animation starts it starts to cut off alot of the image and sometimes becomes very fuzzy. I have tried
public void draw(Canvas canvas,int pointerX, int pointerY) {
Matrix m;
if (setRotation){
// canvas.save();
m = new Matrix();
m.reset();
// spriteWidth and spriteHeight are for just the current frame showed
m.setTranslate(spriteWidth / 2, spriteHeight / 2);
//get and set rotation for ninja based off of joystick
m.preRotate((float) GameControls.getRotation());
//create the rotated bitmap
flipedSprite = Bitmap.createBitmap(bitmap , 0, 0,bitmap.getWidth(),bitmap.getHeight() , m, true);
//set new bitmap to rotated ninja
setBitmap(flipedSprite); // canvas.restore();
Log.d("Ninja View", "angle of rotation= " +(float) GameControls.getRotation());
setRotation = false;
}
And then the Draw Method here
// create the destination rectangle for the ninjas current animation frame
// pointerX and pointerY are from the joystick moving the ninja around
destRect = new Rect(pointerX, pointerY, pointerX + spriteWidth, pointerY + spriteHeight);
canvas.drawBitmap(bitmap, getSourceRect(), destRect, null);
The animation is four frames long and gets incremented by 66 (the size of one of the frames on the bitmap) for every frame and then back to 0 at the end of the loop.
© Game Development or respective owner