Sprite Sheet - 在旧精灵上绘制的新精灵

时间:2014-01-30 14:55:26

标签: android animation canvas surfaceview sprite-sheet

基本上我尝试使用精灵表来制作动画。目标是从1到8计数,间隔为1秒。数字是精灵表上的图像。问题是,一旦数字被绘制到画布上,它就不会消失。新图像刚刚绘制在不需要的旧图像之上。像这样: enter image description here

有没有办法清除已绘制的内容,以便可以在干净的画布上绘制新图像?

DrawStripFrame类:

public class DrawStripFrame extends SurfaceView implements Runnable{

/**variables declared here*/

public DrawStripFrame (Context context){
    super (context);
    this.context = context;
    holder = getHolder();
}

public void setDrawStripFrame(Bitmap bitmap, int width, int height, int columns){
    this.bitmap = bitmap;
    this.width = width;
    this.height = height;
    this.columns = columns;
}
}

@Override
public void run(){
        while(running){         
            if(!holder.getSurface().isValid())
                continue;
                Canvas canvas = holder.lockCanvas();
                draw(canvas);
                holder.unlockCanvasAndPost(canvas);
        }
}

public void draw (Canvas canvas){
    update();
            /**4 being no. of columns and 2 being no. of row*/
    int u = (frame % 4)*(width/4);
    int v = (frame / 4)*(height/2);
    Rect src = new Rect(u,v,u+(width/4),v+(height/2));
    Rect dst = new Rect (0,0, 100,100);
    Paint paint = new Paint();
    canvas.drawBitmap(bitmap, src, dst, paint);
}

public void resume() {
    running = true;
    gameloop = new Thread(this);
    gameloop.start();
}


public void update (){
    frame++;
    if (frame>10)
        frame = 0;
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

1 个答案:

答案 0 :(得分:0)

在你的情况下,你可以用这样的颜色重绘画布:

public void draw (Canvas canvas){
    update();

    // clearing canvas
    canvas.drawColor(Color.BLUE); // whatever color you need it to be        

        /**4 being no. of columns and 2 being no. of row*/
    int u = (frame % 4)*(width/4);
    int v = (frame / 4)*(height/2);
    Rect src = new Rect(u,v,u+(width/4),v+(height/2));
    Rect dst = new Rect (0,0, 100,100);
    Paint paint = new Paint();
    canvas.drawBitmap(bitmap, src, dst, paint);

}