尝试清除Android SurfaceView / Canvas后闪烁
因此,我尝试使用canvas.drawColor(Color.BLACK)清除Canvas,但如果仅调用一次,则显示闪烁并显示应该由drawColor覆盖的旧图形。
这是我代码的重要部分-
// This method is called by a Thread public void update() {
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;
}
在Google搜索一个小贴士之后,我听说了双重缓冲,但是我了解到lockCanvas()和unlockCanvasAndPost()应该为我处理这个问题。这是怎么了?
回答:
我想知道这是否与SurfaceView
双缓冲有关,并且它们交替每帧。因此,您必须至少清除屏幕两帧。
更改toClear = true
成toClear = 2
与集
if(toClear > 0) { 'drawColor'
toClear--;
}
以上是 尝试清除Android SurfaceView / Canvas后闪烁 的全部内容, 来源链接: utcz.com/qa/421606.html