我是否做得不正确或不理解 Android 上的双缓冲?

发布于 2024-11-27 11:26:54 字数 589 浏览 2 评论 0原文

我有一个函数

@Override
public void run() {
    while(running && (!eof)){
        if(surfaceHolder.getSurface().isValid()){
            Canvas canvas = surfaceHolder.lockCanvas();
            paint(canvas);
            surfaceHolder.unlockCanvasAndPost(canvas);  
        }
    }
    thread = null;
}

,其中 Paint(canvas) 调用一堆其他函数来绘制图形和文本,例如,

canvas.drawText("Time="+myRecord.getMyTime(), 100, 100, paint);

我遇到的问题是图形和文本,两者都应该不断变化,而不是被抹掉了,而是继续在自己身上画画。我的整个画布不应该每次都重新绘制吗,因为这就是双缓冲与lock()和unlock()一起工作的方式?我对此理解不正确吗?我该怎么做?

I have a function

@Override
public void run() {
    while(running && (!eof)){
        if(surfaceHolder.getSurface().isValid()){
            Canvas canvas = surfaceHolder.lockCanvas();
            paint(canvas);
            surfaceHolder.unlockCanvasAndPost(canvas);  
        }
    }
    thread = null;
}

where paint(canvas) calls a bunch of other functions that draw a graph and text, for example

canvas.drawText("Time="+myRecord.getMyTime(), 100, 100, paint);

The problem I'm having is that the graph and the text, both of which should be constantly changing, don't get erased but instead keep drawing over themselves. Shouldn't my entire canvas get redrawn every time because that's how double buffering works with the lock() and unlock()? Am I not understanding this correctly? How am I supposed to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪之魂 2024-12-04 11:26:54

您需要在 lockCanvas() 之后使用 Canvas.drawColor() 自行清除 Canvas。

这也可能相关:

在unlockCanvas()之间永远不会保留 Surface 的内容
和lockCanvas(),因此,Surface区域内的每个像素
必须写。此规则的唯一例外是当脏的
指定了矩形,在这种情况下,非脏像素将被
保留。

来源

You need to clear the Canvas yourself after lockCanvas() using Canvas.drawColor().

This might be relevant too:

The content of the Surface is never preserved between unlockCanvas()
and lockCanvas(), for this reason, every pixel within the Surface area
must be written. The only exception to this rule is when a dirty
rectangle is specified, in which case, non-dirty pixels will be
preserved.

Source

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文