Sprite Sheet - 在旧 sprite 之上绘制新 sprite

Posted

技术标签:

【中文标题】Sprite Sheet - 在旧 sprite 之上绘制新 sprite【英文标题】:Sprite Sheet - new sprite being drawn on top of old sprites 【发布时间】:2014-02-22 22:49:58 【问题描述】:

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

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

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】:

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

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

【讨论】:

以上是关于Sprite Sheet - 在旧 sprite 之上绘制新 sprite的主要内容,如果未能解决你的问题,请参考以下文章

Sprite Sheet自动检测单个Sprite界限

unity2d sprite sheet 和 sprite packer 有啥区别?

带有 HTML 5 画布的 Sprite-Sheet BLIT?

Web 应用程序中的 Sprite-sheet 用法 (.NET MVC)

html5 canvas大sprite sheet图片动画性能优化

Sprite Sheet 动画 Cocos2dx