AvoidXferMode to replace a color on canvas

I'm trying to replace a color for something that is drawn on a Canvas using AvoidXferMode. From the android docs it looks like it's exactly what I need:

AvoidXfermode xfermode will draw the src everywhere except on top of the opColor or, depending on the Mode, draw only on top of the opColor.

What I'm trying is something like this:

Paint paint = new Paint();
canvas.drawPaint(paint); // actually drawing a bitmap here
paint.setXferMode(new AvoidXferMode(Color.RED, 0, TARGET);

However, this just gives a red screen, not green as I would expect (replacing the red with green). I guess I'm missing the point some where...Any suggestions?

Asked by: Jared717 | Posted: 20-01-2022

Answer 1

I finally found out what is issue is, there are some clues here: AvoidXferMode Tolerance but it really hit me when I read this post http://stuffthathappens.com/blog/2010/06/04/android-color-banding/ by Eric Burke. The tolerance is failing because the view canvas is not in 8888 mode.

That means that when you draw a color or bitmap on that canvas the colors get converted to the target pixel format and the color might slightly change. To fix this you can either switch the entire window pixel format as seen in Eric's post or you can draw onto a 8888 back buffer.

Unfortunately the link to Eric's post is dead, but Roman Guy also has a similar write up here: http://www.curious-creature.org/2010/12/08/bitmap-quality-banding-and-dithering/

Answered by: Miller299 | Posted: 21-02-2022

Answer 2

I'm currently having the same problem but I got it working by specifying 255 as tolerance instead. According to the API documentation this is wrong (It should draw the destination EVERYWHERE with this full-tolerance setting) but for some reason the value 255 does exactly what the value 0 should do.

Answered by: Kirsten800 | Posted: 21-02-2022

Similar questions

android - AvoidXferMode Tolerance

I have a problem with the following code: protected void onDraw(Canvas canvas) { Paint paint = new Paint(); // Draw a blue circle paint.setColor(Color.BLUE); canvas.drawCircle(100, 100, 50, paint); // Draw a red circle where it collides with the blue one paint.setXfermode(new AvoidXfermode(Color.BLUE, 0, Mode.TARGET)); paint.setColor(Color.RED); canvas.drawCircle(50, 50, 50...

android - AvoidXfermode on api v10 and api v14

In map activity i created overlay on which on Cnavas i draw shapes using Paint Shape consists from points and lines linePaint = new Paint(); linePaint.setColor(mContext.getResources().getColor(R.color.Line)); linePaint.setStyle(Paint.Style.STROKE); linePaint.setStrokeWidth(strokeWidth); linePaint.setXfermode(avoidXfermode); linePaint.setFlags(Paint.ANTI_...

graphics - Android AvoidXfermode is deprecated since API 16, is there a replacement?

I need to draw a bitmap on another bitmap, but I only want to draw on top of pixels that have a specific color (transparent in this case) . I understand that AvoidXfermode could do that, but it is deprecated since API 16. Is there a different method to this now ? Thank you

colors - Android AvoidXferMode Drawing Path Bug?

I am using AvoidXferMode to draw Paths only above the black color, but there is a problem, the border of the paths get colored black and it gets all messed up.. I will show you the photos to better understanding This is the image before draw.. just a black square.. This is the starting of the draw, using Path.

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android