Reputation: 704
When I use Paint
with Color.TRANSPARENT
on a normal 2D canvas in Android, I don't get any results and my intention was to get rid of some of the contents on the canvas. I mean the contents that I want to dispose of don't disappear.
This is the code for my Paint
:
mPointFillPaint = new Paint();
mPointFillPaint.setColor(Color.TRANSPARENT);
mPointFillPaint.setAntiAlias(true);
mPointFillPaint.setStyle(Paint.Style.FILL);
mPointFillPaint.setStrokeJoin(Paint.Join.MITER);
Upvotes: 8
Views: 13551
Reputation: 1208
I found that using
mPaint.setColor(Color.TRANSPARENT);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
or
mPaint.setColor(Color.TRANSPARENT);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
just made my paint black.
I had another approach which was to introduce a transparent color to my colors.xml
<color name="transparentColor">#00ffffff</color>
I opted for the "00ffffff" case but i'm pretty sure that "00000000" will work also, depends on your case.
Final code looks like:
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(getResources().getColor(R.color.transparentColor));
Upvotes: 4
Reputation: 321
The following Paint
configuration should help:
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.TRANSPARENT);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
mPaint.setAntiAlias(true);
Upvotes: 11