how to use DrawFilter?

戏子无情 提交于 2019-12-10 20:21:19

问题


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?


回答1:


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);



回答2:


Study the com.example.android.apis.graphics class in the Android SDK samples directory.



来源:https://stackoverflow.com/questions/13190554/how-to-use-drawfilter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!