简体   繁体   English

如何在TextureView中清除画布(绘制增加的圆圈之后)

[英]How to clear the Canvas (after drawing increasing circles) in a TextureView

I am trying to draw increasing circles in a TextureView. 我正在尝试在TextureView中绘制不断增加的圆圈。 The centre of all circles is the same. 所有圆圈的中心都相同。 I then try to increase the drawn Circle until a specific limit is reached. 然后,我尝试增加绘制的圆,直到达到特定的限制。 Then I want to clear the canvas and start over again. 然后,我想清除画布,然后重新开始。 However using my code (see below), the canvas seems to never be cleared. 但是,使用我的代码(参见下文),似乎永远不会清除画布。 Actually it flashes white shortly when it should be cleared, but then when the first circle in the next cycle is drawn (after attempting to clear canvas), all previous circles reappear and the whole animation seems to go crazy. 实际上,它应该清除时不久会闪烁白色,但是随后当绘制下一个循环中的第一个圆圈时(尝试清除画布后),所有先前的圆圈都重新出现,整个动画似乎变得疯狂。 After letting it run for several seconds I am left with dozens of circles (some overlapping) instead of only approximately 4 per cycle. 让它运行几秒钟后,我剩下几十个圆圈(有些重叠),而不是每个周期只有四个圆圈。 Furthermore they do not have the radius I gave them (basically my code ends up drawing numerous circles of random sizes). 此外,它们没有我给它们的半径(基本上我的代码最终绘制了许多随机大小的圆)。 Spent several days trying different things, but nothing seems to help. 花了几天时间尝试其他事情,但似乎无济于事。

Here's my code: 这是我的代码:

paint.setColor(Color.argb(opac, 177, 177, 177));
            stroke = 5;
            paint.setStrokeWidth(stroke);
            radius = 10;
            Canvas canvas = new Canvas();
            Boolean clear = false;
        //Added these two lines following advice from a previous answer:
        Paint clearPaint = new Paint();
        clearPaint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));

        while (mRunning && !Thread.interrupted()) {

            canvas = mSurface.lockCanvas(null);
            try {   
                if(clear){
                    canvas.drawPaint(clearPaint); //This line should clear the canvas.
                    clear = false;
                }else{                      
                    canvas.drawCircle(circleX, circleY, radius, paint);
                }
            } finally {
                mSurface.unlockCanvasAndPost(canvas);
            }
            if(radius+15 <= circleY-stroke/2){
                radius+=15;
            }else{
                radius = 10;
                clear = true;
            }  
            try {
                Thread.sleep(360);
            } catch (InterruptedException e) {
                // Interrupted
            }

Would really appreciate it if someone could help me out here. 如果有人可以在这里帮助我,我将不胜感激。 I wasn't able to proceed with my project for several weeks now due to this problem. 由于这个问题,我已经几个星期无法继续进行我的项目了。

Create a new Paint Instance for just clearing the canvas 创建一个新的Paint Instance,仅清除画布

Paint clearPaint = new Paint();
clearPaint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));

In your if() block for clearing the canvas, paint it with the above instance of Paint 在用于清除画布的if()块中,使用上述Paint实例对其进行绘制

if(clear){
    canvas.drawPaint(clearPaint);
    clear = false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM