How to draw with an "inverted" paint in Android Canvas?
Asked Answered
T

2

7

I draw some stuff on a canvas, over I want to draw a circle in inverted color :

canvas.drawCircle(zx, zy, 8f, myPaint);

How to configure myPaint for circle pixel to be in the inverted color of the underlying pixels ?

Thanks

Trapes answered 9/5, 2011 at 20:7 Comment(0)
C
4

try this

float mx [] = {
             -1.0f,  0.0f,  0.0f,  1.0f,  0.0f,
             0.0f,  -1.0f,  0.0f,  1.0f,  0.0f,
             0.0f,  0.0f,  -1.0f,  1.0f,  0.0f,
             1.0f,  1.0f,  1.0f,  1.0f,  0.0f 
    };
ColorMatrix cm = new ColorMatrix(mx);

p.setColorFilter(new ColorMatrixColorFilter(cm));

canvas.drawCircle(zx, zy, 8f, p);
Cornstarch answered 9/5, 2011 at 20:31 Comment(4)
I tried with no success :( I get a black circle, whatever the under picture is.Trapes
Sorry, I will try to find other solution.Cornstarch
No problem. It seems like the background color is not taken into account, even when using things like setColorFilter(new PorterDuffColorFilter(Color.BLACK, PorterDuff.Mode.XOR)) or setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR))Trapes
Now I get a static white circle. Do I need to set another âraùeter? ThanksTrapes
M
3

I'd say a color matrix for inverting should look like this:

float mx [] = {
         -1.0f,  0.0f,  0.0f,  0.0f,  255.0f,
         0.0f,  -1.0f,  0.0f,  0.0f,  255.0f,
         0.0f,  0.0f,  -1.0f,  0.0f,  255.0f,
         0.0f,  0.0f,  0.0f,  1.0f,  0.0f 
};

Here is more information for the matrix:

Mosasaur answered 20/6, 2011 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.