Skip to content Skip to sidebar Skip to footer

Color Overlapping When Drawing Multiple Path

ArrayList ArrayList> foregroundPaths = new ArrayList>(); Paint initilization mPaint = new Paint(); mPaint.setAn

Solution 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.

Solution 2:

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

it works for me.

Solution 3:

mPaint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.OVERLAY));

Solution 4:

Since AvoidXferMode is depreciated, this would be helpful for people who are using API 16+. This is rather like a workaround. But works perfectly fine. This overlapping error occurs because of the alpha value set for paint.

For eg., If you want to use 'Red' color with alpha Instead of passing mPaint.setAlpha(), you could actually use utility function to set transparency to 'Red' color.

So in this case, just use mPaint.setColor(Color.parseColor(getTransparentColor(Color.Red, 50))); and don't use setAlpha.

Use below code to get transparent color.

publicstaticString getTransparentColor (int colorCode, int transCode) {
        // convert color code into hexa string and remove starting 2 digitString color = defaultColor;
        try{
            color = Integer.toHexString(colorCode).toUpperCase().substring(2);
        }catch (Exception ignored){}

        if (!color.isEmpty() && transCode < 100) {
            if (color.trim().length() == 6) {
                return"#" + convert(transCode) + color;
            } else {
                Log.d(TAG, "Color is already with transparency");
                return convert(transCode) + color;
            }
        }
        // if color is empty or any other problem occur then we return deafult color;return"#" + Integer.toHexString(defaultColorID).toUpperCase().substring(2);
    }

  /**
     * This method convert numver into hexa number or we can say transparent code
     *
     * @param trans number of transparency you want
     * @return it return hex decimal number or transparency code
     */publicstaticString convert(int trans) {
        String hexString = Integer.toHexString(Math.round(255 * trans / 100));
        return (hexString.length() < 2 ? "0" : "") + hexString;
    }

Note: Even after all these changes if overlapping error occurs, then add the below code.

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

Solution 5:

I had similar problem. I used:

mPaint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.SRC_OVER));

It allows to draw pixels over the destination pixels without making path transparent.

Post a Comment for "Color Overlapping When Drawing Multiple Path"