如何在屏幕中央创建透明的弯曲透明矩形?
我已经编写了以下代码,并在屏幕中心为弯曲的矩形添加了alpha,但是显示的是背景色而不是透明度
A(package a)
答案 0 :(得分:1)
您可以使用{strong> Paint 的setXfermode()
并将PorterDuff.Mode.ADD
和PorterDuff.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);