我希望在屏幕上绘制某些内容后清除画布的内容。
如何完全清除屏幕?任何代码片段都会有所帮助。
感谢。
这是我的代码:
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);
}
}
答案 0 :(得分:3)
试试这个:
canvas.drawColor(0); //use 32bit hex like 0xffffffff for white
或
canvas.drawARGB(0, 0, 0, 0); //0-255 for each component
这将用黑色清除画布。你可以使用任何你喜欢的颜色。
答案 1 :(得分:1)
此代码
canvas.drawARGB(0, 0, 0, 0);
会使画布背景变黑,
但是如果你想完全清除图纸并使画布背景透明,那么请遵循以下代码
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
Rect rect=new Rect(0,0,canvas_width,canvas_height);
canvas.drawRect(rect,paint);