首页 > 代码库 > Android-清除Canvs内容的实现

Android-清除Canvs内容的实现

SurfaceView背景透明:

setZOrderOnTop(true);
SurfaceHolder holder = this.getHolder();holder.setFormat(PixelFormat.TRANSLUCENT);

清除Canvas内容的实现方式一:

Canvas canvas = null;try {  synchronized (holder) {    canvas = holder.lockCanvas(null);    if (canvas == null) {      return;    }    canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);   }} catch (Exception e) {  e.printStackTrace();} finally {  if (canvas != null) {    holder.unlockCanvasAndPost(canvas);  }}

方式二:

 Paint paint = new Paint();   paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));   canvas.drawPaint(paint);   paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));