color overlapping when drawing multiple path

风格不统一 提交于 2019-12-13 07:34:58

问题


ArrayList

ArrayList<Pair<Path, Float>> foregroundPaths = new ArrayList<Pair<Path, Float>>();

Paint initilization

    mPaint = new Paint();
    mPaint.setAntiAlias(false);
    mPaint.setDither(true);
    mPaint.setColor(0x0FFF0000);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.BEVEL);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(Math.abs(ImageViewTouch.brushSize
                    / getScale()));

    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));

    mPaint.setAlpha(0x80);

OnDraw

  canvas.save();


    displayRectF = new RectF();


    canvas.concat(getDisplayMatrix());

    rect = canvas.getClipBounds();
    displayRectF.set(rect);

    for (Pair<Path, Float> p : foregroundPaths) {
        mPaint.setStrokeWidth(p.second);
        canvas.drawPath(p.first, mPaint);
    }



    canvas.restore();

Above codes are capable of drawing on canvas using finger. But the problem is when multiple path crossing each other its overlap. I have attached a link of my app snapshot. Overlapping is inside the green rectangle. I set Xfermode to paint but not working as i expect.

Please help me and suggest me, what should i do to remove this problem. any suggestion will be appreciated. Thanks


回答1:


Have you tried:

mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));

After all what you want is an exclusive OR (XOR) - either the first line or the second line, but not both on top of each other.

I have not tried this, just seems like the logical answer.




回答2:


mPaint.setXfermode(new AvoidXfermode(Color.RED, 90, Mode.AVOID)); 

it works for me.




回答3:


mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.OVERLAY));


来源:https://stackoverflow.com/questions/16522496/color-overlapping-when-drawing-multiple-path

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