Draw a transparent shape on canvas

I have a background image that spans the entire screen. I draw the canvas on top of the background and set its color to white so that you cannot see the image. What I'm trying to achieve is then draw a transparent shape on a white canvas and show the background image where that shape is. I use surfaceView and implement SurfaceView.Callback .

+8
android transparent canvas
source share
2 answers

You should make white color transparent:

 public void draw(Canvas canvas) { final RectF rectF = new RectF(); final Paint paint = new Paint(); paint.setARGB(128, 255, 255, 255); rectF.set(0,0, getMeasuredWidth(), getMeasuredHeight()); canvas.drawRect(rectF, paint); } 
+7
source share

to draw a transparent form, execute this code

 Paint paint = new Paint(); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); //draw any shape, here I am drawing Rect shape Rect rect=new Rect(left, top, right, bottom); canvas.drawRect(rect,paint); 
+7
source share

All Articles