在画布上绘制透明形状

时间:2011-02-25 09:03:44

标签: android canvas transparent

我有一张占据整个屏幕的背景图片。我在背景上绘制画布并将其颜色设置为白色,因此您无法看到图像。我想要实现的是然后在白色画布上绘制一个透明的形状,并通过该形状的位置显示背景图像。我正在使用surfaceView并实现SurfaceView.Callback

2 个答案:

答案 0 :(得分:8)

你应该使白色透明:

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);
}

答案 1 :(得分:8)

绘制透明形状遵循此代码

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);