Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing with Transparent Paint on Android

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); 
like image 542
ax1mx2 Avatar asked Jan 16 '15 11:01

ax1mx2


2 Answers

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);
like image 135
Ateeq Ur Rehman Avatar answered Oct 18 '22 05:10

Ateeq Ur Rehman


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));
like image 29
bko Avatar answered Oct 18 '22 04:10

bko