Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use DrawFilter?

Tags:

android

draw

very little has been discussed about DrawFilter over the net, so i couldnt find a decent answer for the following question:

it has been documanted here that:

A DrawFilter subclass can be installed in a Canvas. When it is present, it can modify the paint that is used to draw (temporarily). With this, a filter can disable/enable antialiasing, or change the color for everything this is drawn.

So i do want to use this feature in order to change the color things are drawn in my canvas to black, but how is it done? since this class has no methods to override its a ridle how i should subclass it to acheive what is documanted...

BTW, to whoever never seen it, canvas has a method canvas.setDrawFilter(DrawFilter) that should be used, i tried to look at the open source code but i didnt get a clue there...

any ideas?

like image 349
Ofek Ron Avatar asked Oct 24 '25 02:10

Ofek Ron


1 Answers

The only available subclass of DrawFilter seems to be exclusively for tweaking bits in Paint flags:

setDrawFilter(new PaintFlagsDrawFilter(Paint.ANTI_ALIAS_FLAG,Paint.DITHER_FLAG))

So, I doubt it is of much use.

Also, check out ColorFilter. And setSaturation() of ColorMatrix. That might help drawing in grayscale.

Example: to draw an colored image (inImg) as black and white (outImg of size size) on a canvas:

    Bitmap outImg = Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565);
    Canvas can = new Canvas(outImg);
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    Paint pnt = new Paint(Paint.DITHER_FLAG);
    pnt.setColorFilter(new ColorMatrixColorFilter(cm));
    can.drawBitmap(inImg, 0, 0,pnt);
like image 54
S.D. Avatar answered Oct 26 '25 17:10

S.D.