如何清除Android中绘制的Canvas的内容?

我想在屏幕上绘制某些东西后清除画布的内容.

如何完全清除屏幕?任何代码片段都会有所帮助.

谢谢.

这是我的代码:

public class Panel extends SurfaceView implements SurfaceHolder.Callback {

private ViewThread mThread;
private ArrayList<Element> mElements = new ArrayList<Element>();


public Panel(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    this.setBackgroundColor(Color.TRANSPARENT);                 
    this.setZOrderOnTop(true); //necessary                
    getHolder().setFormat(PixelFormat.TRANSPARENT); 
    getHolder().addCallback(this); 
    mThread = new ViewThread(this); 

} 


public void doDraw(Canvas canvas) {
     super.onDraw(canvas);

     //canvas.drawColor(Color.TRANSPARENT);
    // canvas.drawColor(Color.argb(0, 255, 255, 255)); 
    //canvas.drawColor(Color.rgb(-1, -1, -1));
    //canvas.drawARGB(0, 255, 255, 255);
    synchronized (mElements) {
        for (Element element : mElements) {
            element.doDraw(canvas);
        }
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    // TODO Auto-generated method stub
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    if (!mThread.isAlive()) {
        mThread = new ViewThread(this);
        mThread.setRunning(true);
        mThread.start();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    if (mThread.isAlive()) {
        mThread.setRunning(false);
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    synchronized (mElements) {
        mElements.add(new Element(getResources(), (int) event.getX(), (int) event.getY()));
    }
    return super.onTouchEvent(event);
}

}

解决方法:

试试这个 :

canvas.drawColor(0); //use 32bit hex like 0xffffffff for white

要么

canvas.drawARGB(0, 0, 0, 0); //0-255 for each component

这将用黑色清除画布.你可以使用任何你喜欢的颜色.

上一篇:使用代码创建ABAP transparent table


下一篇:qt清除输出文件