Paint 滤镜简单流程 :
颜色矩阵 ( 4 行 5 列矩阵 ) 写出对应的 float 数组 ( 20个元素 ) ;
new float[]{
0.213f, 0.715f, 0.072f, 0, 0,
0.213f, 0.715f, 0.072f, 0, 0,
0.213f, 0.715f, 0.072f, 0, 0,
0, 0, 0, 1, 0,
}
ColorMatrix matrix = new ColorMatrix(new float[]{
0.213f, 0.715f, 0.072f, 0, 0,
0.213f, 0.715f, 0.072f, 0, 0,
0.213f, 0.715f, 0.072f, 0, 0,
0, 0, 0, 1, 0,
});
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
paint.setColorFilter(filter);
之后可以使用画笔进行绘制 ;
Paint 滤镜使用流程 :
颜色矩阵 ( 4 行 5 列矩阵 ) 写出对应的 float 数组 ( 20个元素 ) ;
new float[]{
0.213f, 0.715f, 0.072f, 0, 0,
0.213f, 0.715f, 0.072f, 0, 0,
0.213f, 0.715f, 0.072f, 0, 0,
0, 0, 0, 1, 0,
}
的矩阵 , 在 Java 代码中使用 一个 20个元素 float[] 数组表示 ;
ColorMatrix matrix = new ColorMatrix(new float[]{
0.213f, 0.715f, 0.072f, 0, 0,
0.213f, 0.715f, 0.072f, 0, 0,
0.213f, 0.715f, 0.072f, 0, 0,
0, 0, 0, 1, 0,
});
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
/**
* 设置滤镜时的画笔
*/
private Paint paint;
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColorFilter(filter);
private Bitmap bitmap;
bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.trump);
RectF rectF = new RectF(0,0,getWidth(), getHeight());
canvas.drawBitmap(bitmap, null , rectF, paint);
package net.csdn.blog.hanshuliang.filter;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
/**
* 滤镜示例 : 黑白照片效果
*
* 将 RGBA 某个通道的值翻倍 , 即将对应的颜色矩阵值设置成对应的倍数
* 第 1 行 第 1 列 : R ( Red ) , 红色通道倍数 ;
* 第 2 行 第 2 列 : G ( Green ) , 绿色通道倍数 ;
* 第 3 行 第 3 列 : B ( Blue ) , 蓝色通道倍数 ;
* 第 4 行 第 4 列 : A ( Alpha ) , 透明度通道倍数 ;
*
* 通道增量 :
* 第 1 行 第 1 列 : R ( Red ) , 红色通道倍数 ;
* 第 2 行 第 2 列 : G ( Green ) , 绿色通道倍数 ;
* 第 3 行 第 3 列 : B ( Blue ) , 蓝色通道倍数 ;
* 第 4 行 第 4 列 : A ( Alpha ) , 透明度通道倍数 ;
*
*/
public class PaintFilterE extends View {
/**
* 设置滤镜时的画笔
*/
private Paint paint;
/**
* 使用滤镜处理的图像
*/
private Bitmap bitmap;
public PaintFilterE(Context context) {
super(context);
}
public PaintFilterE(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
// 创建画笔 , 并打开抗锯齿
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// 加载图像资源
bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.trump);
}
public PaintFilterE(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// ① 设置颜色矩阵 , 黑白照片效果
ColorMatrix matrix = new ColorMatrix(new float[]{
0.213f, 0.715f, 0.072f, 0, 0,
0.213f, 0.715f, 0.072f, 0, 0,
0.213f, 0.715f, 0.072f, 0, 0,
0, 0, 0, 1, 0,
});
// ② 根据颜色矩阵创建滤镜
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
// ③ 绘制区域
RectF rectF = new RectF(0,0,getWidth(), getHeight());
// ④ 设置滤镜
paint.setColorFilter(filter);
// ⑤ 绘制经过滤镜处理的图片
canvas.drawBitmap(bitmap, null , rectF, paint);
}
}
① 颜色矩阵代码 :
// ① 设置颜色矩阵 , 该矩阵将 红色通道的值 翻倍
ColorMatrix matrix = new ColorMatrix(new float[]{
2, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0,
});
① 颜色矩阵代码 :
// ① 设置颜色矩阵 , 该矩阵将 红色通道的值 增加 30
ColorMatrix matrix = new ColorMatrix(new float[]{
1, 0, 0, 0, 30,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0,
});
① 颜色矩阵代码 :
// ① 设置颜色矩阵 , 底片效果
ColorMatrix matrix = new ColorMatrix(new float[]{
-1, 0, 0, 0, 255,
0, -1, 0, 0, 255,
0, 0, -1, 0, 255,
0, 0, 0, 1, 0,
});
红绿通道交换效果
① 颜色矩阵代码 :
// ① 设置颜色矩阵 , 该矩阵将 红色 和 蓝色通道交换
ColorMatrix matrix = new ColorMatrix(new float[]{
0, 1, 0, 0, 0,
1, 0, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0,
});
黑白照片效果
① 颜色矩阵代码 :
// ① 设置颜色矩阵 , 黑白照片效果
ColorMatrix matrix = new ColorMatrix(new float[]{
0.213f, 0.715f, 0.072f, 0, 0,
0.213f, 0.715f, 0.072f, 0, 0,
0.213f, 0.715f, 0.072f, 0, 0,
0, 0, 0, 1, 0,
});
复古效果
① 颜色矩阵代码 :
// ① 设置颜色矩阵 , 复古效果
ColorMatrix matrix = new ColorMatrix(new float[]{
1/2f, 1/2f, 1/2f, 0, 0,
1/3f, 1/3f, 1/3f, 0, 0,
1/4f, 1/4f, 1/4f, 0, 0,
0, 0, 0, 1, 0,
});
美颜效果
① 颜色矩阵代码 :
// ① 设置颜色矩阵 , 美颜效果
ColorMatrix matrix = new ColorMatrix(new float[]{
1.3f, 0, 0, 0, 0,
0, 1.3f, 0, 0, 0,
0, 0, 1.3f, 0, 0,
0, 0, 0, 1, 0,
});
相关代码地址 :