Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

draw transparent shape onto canvas

I have a background image that takes up the whole screen. I am drawing canvas over the background and setting its color to white so you can't see the image yet. What I am trying to achieve is to then draw a transparent shape onto the white canvas and have the background image show through where that shape is. I am using a surfaceView and implementing SurfaceView.Callback.

like image 366
pengume Avatar asked Feb 25 '11 09:02

pengume


2 Answers

to draw transparent shape follow 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);
like image 120
Swarnendu Paul Avatar answered Oct 18 '22 23:10

Swarnendu Paul


You should make the 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);
}
like image 35
david Avatar answered Oct 18 '22 23:10

david