0

Basically I tried to use a sprite sheet for animation. The aim is to count from 1 to 8 with 1 second interval. The numbers are image on a sprite sheet. The problem is once the number is being drawn to the canvas, it just won't go away. New image just drawn on top of the unwanted old image. Like this: enter image description here

Is there a way to clear what has been drawn, so that new image can be drawn on a clean canvas?

DrawStripFrame Class:

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();
    }
}
}
Wallyfull
  • 181
  • 1
  • 2
  • 16

1 Answers1

0

In your case you could just redraw the canvas with a color like this:

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);

}

stackdaddy
  • 111
  • 1
  • 10