This example describes the use of Android sprite animations. Share to everyone for your reference. Specifically as follows:
Elaineanimated.java files are as follows:
Package Net.obviam.walking.model;
Import Android.graphics.Bitmap;
Import Android.graphics.Canvas;
Import Android.graphics.Paint;
Import Android.graphics.Rect;
public class Elaineanimated {private static final String TAG = ElaineAnimated.class.getSimpleName ();
Private Bitmap Bitmap;
The animation sequence private Rect sourcerect;
The rectangle to is drawn from the animation bitmap private int framenr;
Number of frames in animation private int currentframe;
The current frame private long frameticker;
The time of the last frame update private int frameperiod;
Milliseconds between each frame (1000/fps) private int spritewidth;
The width of the sprite to calculate the cut out rectangle private int spriteheight;
The height of the sprite private int x;
The X coordinate of the object (top left of the image) private int y; The Y coordinate of the object (top left of the image) is public elaineanimated (Bitmap Bitmap, int x, int y, int width, int height, int fps, int framecount) {this.bitmap = bitmap;
this.x = x;
This.y = y;
currentframe = 0;
Framenr = Framecount;
Spritewidth = Bitmap.getwidth ()/framecount;
Spriteheight = Bitmap.getheight ();
Sourcerect = new Rect (0, 0, spritewidth, spriteheight);
Frameperiod = 1000/fps;
Frameticker = 0l;
Public Bitmap Getbitmap () {return Bitmap;
public void SetBitmap (Bitmap Bitmap) {this.bitmap = Bitmap;
Public Rect Getsourcerect () {return sourcerect;
public void Setsourcerect (Rect sourcerect) {this.sourcerect = Sourcerect;
public int Getframenr () {return framenr;
The public void setframenr (int framenr) {this.framenr = Framenr;
public int Getcurrentframe () {return currentframe;
The public void setcurrentframe (int currentframe) {this.currentframe = Currentframe;
public int Getframeperiod () {return frameperiod; } public void SetframeperIOD (int frameperiod) {this.frameperiod = Frameperiod;
public int getspritewidth () {return spritewidth;
The public void setspritewidth (int spritewidth) {this.spritewidth = Spritewidth;
public int getspriteheight () {return spriteheight;
The public void setspriteheight (int spriteheight) {this.spriteheight = Spriteheight;
public int GetX () {return x;
public void SetX (int x) {this.x = x;
public int GetY () {return y;
The public void sety (int y) {this.y = y; }//The Update method to Elaine public void update (long gametime) {if (Gametime > Frameticker + frameperiod)
{frameticker = gametime;
Increment the frame currentframe++;
if (currentframe >= framenr) {currentframe = 0;
}//define the rectangle to cut out sprite This.sourceRect.left = currentframe * spritewidth;
This.sourceRect.right = This.sourceRect.left + spritewidth; }//THe draw method which draws the corresponding frame public void draw (Canvas Canvas) {//Where to draw the sprite
Rect destrect = new Rect (GetX (), GetY (), GetX () + Spritewidth, GetY () + spriteheight);
Canvas.drawbitmap (Bitmap, sourcerect, destrect, NULL);
Canvas.drawbitmap (bitmap, M, null);
Paint Paint = new Paint ();
Paint.setargb (50, 0, 255, 0); Canvas.drawrect (Currentframe * destrect.width ()), + (Currentframe * destrect.width ()) + destrect.width (), 15
0 + destrect.height (), paint);
}
}
I hope this article will help you with your Android program.