android 下滤镜效果的实现

滤镜过滤颜色已实现,简单版本可通过下面代码的3个参数实现黑白、红、绿。。。等7种过滤(RGB的7种组合)。
 
理论上讲可以过滤为任意颜色。调整混合结果的比值就行了。
 
 
code:
public void drawBitmap(Canvas canvas, Bitmap bitmap, boolean r, boolean g, boolean b) {
ColorMatrix cm = new ColorMatrix();
float[] m = cm.getArray();
setColorFilterMatrix(m, r, g, b);
Paint pt = new Paint();
pt.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, 0, 0, pt);
}

public void setColorFilterMatrix(float[] m, boolean r, boolean g, boolean b) {
final float R = 0.213f;
final float G = 0.715f;
final float B = 0.072f;

m[0] = 0;
m[6] = 0;
m[12] = 0;

if (r) {
m[0] = R; m[1] = G; m[2] = B;
}
if (g) {
m[5] = R; m[6] = G; m[7] = B;
}
if (b) {
m[10] = R; m[11] = G; m[12] = B;
}
}
posted @ 2012-01-04 17:12  图形学小菜鸟  阅读(3760)  评论(0编辑  收藏  举报