How to create a transparent curved transparent rectangle at center of the screen?

Aj08 :

How to create a transparent curved transparent rectangle at center of the screen?

I have written the following code and added alpha for the curved rectangle at center of the screen, but the background color is being shown instead of transparency

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

You can make use of setXfermode() of Paint and pass PorterDuff.Mode.ADD and PorterDuff.Mode.ADD as parameter in order to obtain inner transparency region instead of adding alpha to paint.

Make the following changes in your code:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=106429&siteId=1