gpt4 book ai didi

java - 如何在屏幕中心创建一个透明的弯曲透明矩形?

转载 作者:太空狗 更新时间:2023-10-29 13:46:07 24 4
gpt4 key购买 nike

如何在屏幕中心创建一个透明弯曲的透明矩形?

我已经编写了以下代码并为屏幕中心的弯曲矩形添加了 alpha,但是显示的是背景颜色而不是透明度

int width = canvas.getWidth();
int height = canvas.getHeight();
Rect childRect = this.getChildRect();

Paint outerPaint = new Paint();
outerPaint.setColor(Color.LTGRAY);
borderPaint.setStyle(Paint.Style.FILL);


Paint innerPaint = new Paint();
innerPaint.setARGB(0, 0, 0, 0);
innerPaint.setAlpha(0);
innerPaint.setStyle(Paint.Style.FILL);

canvas.drawRect(0.0F, 0.0F, width, height, outerPaint);
canvas.drawRoundRect(new RectF(childRect.left, childRect.top, childRect.right, childRect.bottom), 8F, 8F, innerPaint);

最佳答案

您可以使用PaintsetXfermode()并传递PorterDuff.Mode.ADDPorterDuff.Mode.ADD 作为参数以获得内部透明区域而不是将 alpha 添加到绘画。

在您的代码中进行以下更改:

int width = canvas.getWidth();
int height = canvas.getHeight();
Rect childRect = this.getChildRect();

Paint outerPaint = new Paint();
outerPaint.setColor(Color.LTGRAY);
outerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
outerPaint.setAntiAlias(true);

Paint innerPaint = new Paint();
innerPaint.setColor(Color.TRANSPARENT);
innerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
innerPaint.setAntiAlias(true);

canvas.drawRect(0.0F, 0.0F, width, height, outerPaint);
canvas.drawRoundRect(new RectF(childRect.left, childRect.top, childRect.right, childRect.bottom), 8F, 8F, innerPaint);

关于java - 如何在屏幕中心创建一个透明的弯曲透明矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54130193/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com