Animating with rotation
I'm writing a game and now I'm starting to realize that the performance has to be improved (link to game (market)).
If you can't check it out: It's a snake-like game with birds. You control the first bird (by drawing a path for it to follow / using dpad), and a swarm of smaller birds follows it in line. The birds are animated, and can be rotated by 180° and mirrored (depending where they're flying through).
At the moment I animate the first bird only, then scale it down and save it in an invisible element which is shown 4 frames later (fluid animation purposes), instead of animating each bird individually. So for each bird you see on screen, there are 4 objects with a bitmap each. Now my question is, should I make a spritesheet and reduce the number of possible rotations (say, 1 se开发者_StackOverflowt of sprites every 10°) or calculate the animation for each bird, or keep it my way?
There is no point making your rotated sprite sheets by hand, I'd just load in a single sprite sheet. Then make an array of Bitmaps, for each angle, and rotate and copy the base sprite sheets over each. (you may need an array of arrays to handle the animations, or do separate rotates for each frame of the sprite sheet) This also lets you change your art later with ease.
something like:
Bitmap baseFrame;
Bitmap rotatedFrame[]=new Bitmap[360/10];
...
Matrix rotationMatrix=new Matrix();
rotationMatrix.setRotate(n*10);
rotatedFrame[n]=Bitmap.createBitmap(baseFrame,0,0,
baseFrame.getWidth(), baseFrame.getHeight(),
rotationMatrix, false);
The width, and height of the rotated images may need to be larger then to base image, and you may want you set it up to rotate around the centre but you get the idea
By the way,
Having a look at your screenshots of your game I suspect that drawing your background may have a bigger effect then rotating the birds. You may want to profile your code to see how long it's taking to draw each image. ( http://developer.android.com/guide/developing/debugging/debugging-tracing.html ). I'd make sure your background is being loaded in a RGB_565 format, it can be quite a lot faster when rendering in software.
I have found rotating images in android to be surprisingly fast, being able to rotate hundreds of small bitmaps at a good frame rate.
Here is the simple explanation of the Rotation Animation:
http://androidtutorials60.blogspot.in/2013/09/simple-rotate-animation-in-android.html
精彩评论