Android - Canvas Paint - only draw over White/Transparent

537 Views Asked by At

i need to draw a Rectangle only over the areas, which were not drawn yet.

For example when i call drawText to write something, and then draw the Rect over it, it should be behind the text.

I cant simply first draw the Rect, then write the Text in that case.

I read about XFermode, but i do not know, how to use it...

1

There are 1 best solutions below

0
tato.rodrigo On

You can setup your paint object to use PorterDuffXfermode with blend mode Mode.MULTIPLY.

import android.graphics.PorterDuffXfermode;
import android.graphics.PorterDuff.Mode;

private void setupPaint(Paint paint) {
    paint.setXfermode(new PorterDuffXfermode(Mode.MULTIPLY));
}

private void clearXfermode(Paint paint) {
    paint.setXfermode(null);
}

In my opinion it is better for performance to draw the rectangle first and then draw the text, but if this is not possible then use PorterDuffXfermode.