我创建了一个精灵类,在这个例子中它运行了一个44帧的spritesheet,我想将帧速率设置为10fps,所以所有动画将是4秒或更少。 这是我的代码,它工作但速度非常慢,我不明白为什么:
package sprite2.sprite2;
import sprite2.sprite2.Sprite2Activity.Panel;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
public class Sprite {
int x,y;
int xSpeed, ySpeed;
int height, width;
Bitmap b;
Panel ov;
int currentFrame = 0;
int direction = 0;
int framePeriod;
long frameTicker;
int frameNr;
public Sprite(Panel panel, Bitmap _scratch) {
b = _scratch;
ov = panel;
height = 480;
width = b.getWidth()/44;
x = y = 0;
ySpeed = 0;
framePeriod = 1000 / 10;
frameTicker = 0;
frameNr = 44;
// TODO Auto-generated constructor stub
}
public void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
update(System.currentTimeMillis());
int srcX = currentFrame * width;
Rect src = new Rect (srcX, 0 , srcX+width, height);
Rect dest = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());
canvas.drawBitmap(b, src, dest, null);
}
public void update(long videotime) {
if (videotime > frameTicker + framePeriod) {
frameTicker = videotime;
// increment the frame
currentFrame++;
if (currentFrame >= frameNr) {
currentFrame = 0;
}
}
}
}