How to fill rectangle with opacity in android

2019-04-05 03:42发布

问题:

I can fill rectangle with canvas draw rect:

Rect rt = new Rect(0, 0, getWidth(), getHeight());
myPaint.setColor(myColor);
myPaint.setStyle(Style.FILL);
canvas.drawRect(rt, myPaint);

But I need the method to fill rectangle with the opacity (in percent, with 0% is TRANSPARENT).
How can I do that?

回答1:

You can use the Alpha property of Paint class.

myPaint.setAlpha(10); will help you.