Reputation: 5033
I would like to clear the contents of the canvas after drawing certain things on the screen.
How do I clear the screen fully? Any snippets of code on it will be helpful.
Thanks.
This is my code:
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);
}
}
Upvotes: 4
Views: 6785
Reputation: 4137
this code
canvas.drawARGB(0, 0, 0, 0);
will make canvas background black,
But if you want to clear the drawing totally and make the canvas background transparent then follow this code
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);
Upvotes: 1
Reputation: 61538
Try this :
canvas.drawColor(0); //use 32bit hex like 0xffffffff for white
or
canvas.drawARGB(0, 0, 0, 0); //0-255 for each component
This will clear the canvas with black. You can use any color you like.
Upvotes: 5