myq_26
myq_26
采纳率80%
2013-07-17 06:19 阅读 3.4k

试图清除 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() 可以处理这个问题。现在是哪里出错了呢?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

相关推荐