Android精灵动画用法实例
本文实例讲述了Android精灵动画用法。分享给大家供大家参考。具体如下:
ElaineAnimated.java文件如下:
packagenet.obviam.walking.model; importandroid.graphics.Bitmap; importandroid.graphics.Canvas; importandroid.graphics.Paint; importandroid.graphics.Rect; publicclassElaineAnimated{ privatestaticfinalStringTAG=ElaineAnimated.class.getSimpleName(); privateBitmapbitmap; //theanimationsequence privateRectsourceRect; //therectangletobedrawnfromtheanimationbitmap privateintframeNr; //numberofframesinanimation privateintcurrentFrame; //thecurrentframe privatelongframeTicker; //thetimeofthelastframeupdate privateintframePeriod; //millisecondsbetweeneachframe(1000/fps) privateintspriteWidth; //thewidthofthespritetocalculatethecutoutrectangle privateintspriteHeight; //theheightofthesprite privateintx; //theXcoordinateoftheobject(topleftoftheimage) privateinty; //theYcoordinateoftheobject(topleftoftheimage) publicElaineAnimated(Bitmapbitmap,intx,inty,intwidth,intheight,intfps,intframeCount){ this.bitmap=bitmap; this.x=x; this.y=y; currentFrame=0; frameNr=frameCount; spriteWidth=bitmap.getWidth()/frameCount; spriteHeight=bitmap.getHeight(); sourceRect=newRect(0,0,spriteWidth,spriteHeight); framePeriod=1000/fps; frameTicker=0l; } publicBitmapgetBitmap(){ returnbitmap; } publicvoidsetBitmap(Bitmapbitmap){ this.bitmap=bitmap; } publicRectgetSourceRect(){ returnsourceRect; } publicvoidsetSourceRect(RectsourceRect){ this.sourceRect=sourceRect; } publicintgetFrameNr(){ returnframeNr; } publicvoidsetFrameNr(intframeNr){ this.frameNr=frameNr; } publicintgetCurrentFrame(){ returncurrentFrame; } publicvoidsetCurrentFrame(intcurrentFrame){ this.currentFrame=currentFrame; } publicintgetFramePeriod(){ returnframePeriod; } publicvoidsetFramePeriod(intframePeriod){ this.framePeriod=framePeriod; } publicintgetSpriteWidth(){ returnspriteWidth; } publicvoidsetSpriteWidth(intspriteWidth){ this.spriteWidth=spriteWidth; } publicintgetSpriteHeight(){ returnspriteHeight; } publicvoidsetSpriteHeight(intspriteHeight){ this.spriteHeight=spriteHeight; } publicintgetX(){ returnx; } publicvoidsetX(intx){ this.x=x; } publicintgetY(){ returny; } publicvoidsetY(inty){ this.y=y; } //theupdatemethodforElaine publicvoidupdate(longgameTime){ if(gameTime>frameTicker+framePeriod){ frameTicker=gameTime; //incrementtheframe currentFrame++; if(currentFrame>=frameNr){ currentFrame=0; } } //definetherectangletocutoutsprite this.sourceRect.left=currentFrame*spriteWidth; this.sourceRect.right=this.sourceRect.left+spriteWidth; } //thedrawmethodwhichdrawsthecorrespondingframe publicvoiddraw(Canvascanvas){ //wheretodrawthesprite RectdestRect=newRect(getX(),getY(),getX()+spriteWidth,getY()+spriteHeight); canvas.drawBitmap(bitmap,sourceRect,destRect,null); canvas.drawBitmap(bitmap,20,150,null); Paintpaint=newPaint(); paint.setARGB(50,0,255,0); canvas.drawRect(20+(currentFrame*destRect.width()),150,20+(currentFrame*destRect.width())+destRect.width(),150+destRect.height(),paint); } }
希望本文所述对大家的Android程序设计有所帮助。