color overlapping when drawing multiple path

2019-09-06 21:10发布

问题:

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