在画布上绘制透明形状

发布于 2024-10-19 04:48:23 字数 139 浏览 1 评论 0原文

我有一个占据整个屏幕的背景图像。我正在背景上绘制画布并将其颜色设置为白色,这样您还看不到图像。我想要实现的目标是在白色画布上绘制一个透明形状,并让背景图像显示该形状所在的位置。我正在使用 SurfaceView 并实现 SurfaceView.Callback 。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天赋异禀 2024-10-26 04:48:23

要绘制透明形状,请遵循此代码

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

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);
风渺 2024-10-26 04:48:23

您应该使白色透明:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文