Drawing with Transparent Paint on Android

这一生的挚爱 提交于 2020-01-24 04:37:21

问题


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

回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/27982853/drawing-with-transparent-paint-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!