2014-01-30 73 views
0

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

是否有一种方法可以清除已绘制的内容,以便可以在干净的画布上绘制新图像?

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

回答

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

}