2 myq 26 myq_26 于 2013.07.17 14:19 提问

试图清除 Android SurfaceView/Canvas 时闪烁

我想使用 canvas.drawColor(Color.BLACK) 来清除Canvas,但是如果我只调用一次,显示闪烁,显示旧的图画,这个图画应该被drawColor覆盖。
部分代码:

 public void update() {
   //This method is called by a Thread                
    Canvas canvas = holder.lockCanvas(null);
    if (canvas != null) {
        onDraw(canvas);
    }
    holder.unlockCanvasAndPost(canvas);
}
@Override
protected void onDraw(Canvas canvas) {
    if (toClear) {
        canvas.drawColor(Color.BLACK);

            //if this is not set to change back to false, it does not flicker
        toClear = false;
    }
    //Draw some objects that are moving around
}
public void clearScreen() {
    //This method is called when the user pressed a button
     toClear = true;
}

我查找了关于double buffering的资料,了解到lockCanvas() 和 unlockCanvasAndPost() 可以处理这个问题。现在是哪里出错了呢?

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!