Android精灵动画用法实例
程序员文章站
2023-10-28 20:09:22
本文实例讲述了android精灵动画用法。分享给大家供大家参考。具体如下:
elaineanimated.java文件如下:
package net.obvia...
本文实例讲述了android精灵动画用法。分享给大家供大家参考。具体如下:
elaineanimated.java文件如下:
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 be 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) 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; } public void setframenr(int framenr) { this.framenr = framenr; } public int getcurrentframe() { return currentframe; } 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; } public void setspritewidth(int spritewidth) { this.spritewidth = spritewidth; } public int getspriteheight() { return spriteheight; } 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; } public void sety(int y) { this.y = y; } // the update method for 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, 20, 150, null); paint paint = new paint(); paint.setargb(50, 0, 255, 0); canvas.drawrect(20 + (currentframe * destrect.width()), 150, 20 + (currentframe * destrect.width()) + destrect.width(), 150 + destrect.height(), paint); } }
希望本文所述对大家的android程序设计有所帮助。