LightingColorFilter 滤镜
构造方法:
LightingColorFilter(int mul, int add)
mul和add都是和颜色值格式相同的int值,其中mul用来和目标像素相乘,add用来和目标像素相加:
R’ = R * mul.R / 0xff + add.R
G’ = G * mul.G / 0xff + add.G
B’ = B * mul.B / 0xff + add.B
使用:
LightingColorFilter lighting = new LightingColorFilter(0xffffff,0x000000);
mPaint.setColorFilter(lighting);
canvas.drawBitmap(mBitmap, 0,0, mPaint);
// 原始图片效果
private void showOriginBitmap(Canvas canvas) {
// mul进行乘法运算,add表明不偏移
LightingColorFilter lighting = new LightingColorFilter(0xffffff,0x000000);
mPaint.setColorFilter(lighting);
canvas.drawBitmap(mBitmap, 0,0, mPaint);
}
// 过滤红色效果
private void filterRedBitmap(Canvas canvas) {
// mul过滤红色,add表明不偏移
LightingColorFilter lighting = new LightingColorFilter(0x00ffff,0x000000);
mPaint.setColorFilter(lighting);
canvas.drawBitmap(mBitmap, 0,0, mPaint);
}
// 过滤绿色效果
private void filterGreenBitmap(Canvas canvas) {
// mul过滤绿色,add表明不偏移
LightingColorFilter lighting = new LightingColorFilter(0xff00ff,0x000000);
mPaint.setColorFilter(lighting);
canvas.drawBitmap(mBitmap, 0,0, mPaint);
}
// 过滤蓝色效果
private void filterBlueBitmap(Canvas canvas) {
// mul过滤蓝色,add表明不偏移
LightingColorFilter lighting = new LightingColorFilter(0xffff00,0x000000);
mPaint.setColorFilter(lighting);
canvas.drawBitmap(mBitmap, 0,0, mPaint);
}
// 红色更亮效果
private void redLightingBitmap(Canvas canvas) {
// mul不过滤颜色,add表明红色更亮
LightingColorFilter lighting = new LightingColorFilter(0xffffff,0x300000);
mPaint.setColorFilter(lighting);
canvas.drawBitmap(mBitmap, 0,0, mPaint);
}
// 绿色更亮效果
private void greenLightingBitmap(Canvas canvas) {
// mul不过滤颜色,add表明绿色更亮
LightingColorFilter lighting = new LightingColorFilter(0xffffff,0x003000);
mPaint.setColorFilter(lighting);
canvas.drawBitmap(mBitmap, 0,0, mPaint);
}
// 蓝色更亮效果
private void blueLightingBitmap(Canvas canvas) {
// mul不过滤颜色,add表明蓝色更亮
LightingColorFilter lighting = new LightingColorFilter(0xffffff,0x000030);
mPaint.setColorFilter(lighting);
canvas.drawBitmap(mBitmap, 0,0, mPaint);
}
PoterDuffColorFilter 滤镜
构造方法:
PorterDuffColorFilter(int color, PorterDuff.Mode mode)
color 具体的颜色值,例如:Color.RED。mode 指定图层混合模式。
bitmap就是dst,传入的color就是src。即底层是bitmap,上层是color层,最后混合效果呈现到src上。
使用:
PorterDuffColorFilter porterDuffColorFilter =
new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.DARKEN);
mPaint.setColorFilter(porterDuffColorFilter);
canvas.drawBitmap(mBitmap, 0, 0, mPaint);
//效果作用于src源图像区域
private static final PorterDuff.Mode[] pdModes = {
//所绘制不会提交到画布上
PorterDuff.Mode.CLEAR,
//显示上层绘制的图像
PorterDuff.Mode.SRC,
//显示下层绘制图像
PorterDuff.Mode.DST,
//正常绘制显示,上下层绘制叠盖
PorterDuff.Mode.SRC_OVER,
//上下层都显示,下层居上显示
PorterDuff.Mode.DST_OVER,
//取两层绘制交集,显示上层
PorterDuff.Mode.SRC_IN,
//取两层绘制交集,显示下层
PorterDuff.Mode.DST_IN,
//取上层绘制非交集部分,交集部分变成透明
PorterDuff.Mode.SRC_OUT,
//取下层绘制非交集部分,交集部分变成透明
PorterDuff.Mode.DST_OUT,
//取上层交集部分与下层非交集部分
PorterDuff.Mode.SRC_ATOP,
//取下层交集部分与上层非交集部分
PorterDuff.Mode.DST_ATOP,
//去除两图层交集部分
PorterDuff.Mode.XOR,
//取两图层全部区域,交集部分饱和度相加
PorterDuff.Mode.ADD,
//取两图层交集部分,颜色叠加
PorterDuff.Mode.MULTIPLY,
//取两图层全部区域,交集部分滤色
PorterDuff.Mode.SCREEN,
//取两图层全部区域,交集部分叠加
PorterDuff.Mode.OVERLAY,
//取两图层全部区域,交集部分颜色加深
PorterDuff.Mode.DARKEN,
//取两图层全部区域,交集部分颜色点亮
PorterDuff.Mode.LIGHTEN
};
private static final String[] labels = {
"Clear", "Src", "Dst", "SrcOver", "DstOver", "SrcIn",
"DstIn", "SrcOut", "DstOut", "SrcATop", "DstATop", "Xor",
"Add", "Multiply", "Screen", "Overlay", "Darken", "Lighten"
};
static Bitmap makeDst(Context context, int w, int h) {
Bitmap tempBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.horse)
.copy(Bitmap.Config.ARGB_8888, true);
return scaleBitmap(tempBitmap, w, h);
}
/** 按比例缩放图片 */
private static Bitmap scaleBitmap(Bitmap origin, int w, int h) {
if (origin == null) {
return null;
}
int width = origin.getWidth();
int height = origin.getHeight();
float wRatio = (float) w/width;
float hRatio = (float) h/height;
Matrix matrix = new Matrix();
matrix.preScale(wRatio, hRatio);
Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
if (newBM.equals(origin)) {
return newBM;
}
origin.recycle();
return newBM;
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(0x00ffffff);
Paint labelP = new Paint(Paint.ANTI_ALIAS_FLAG);
labelP.setTextAlign(Paint.Align.CENTER);
labelP.setTextSize(30);
Paint paint = new Paint();
canvas.translate(column_space, row_space);
int x = 0;
int y = 0;
for (int i = 0; i < pdModes.length; i++) {
// draw the src/dst example into our offscreen bitmap
int sc = canvas.saveLayer(x, y, x + imgW, y + imgH, null, Canvas.ALL_SAVE_FLAG);
canvas.translate(x, y);
PorterDuffColorFilter porterDuffColorFilter =
new PorterDuffColorFilter(0xFFDD4040, pdModes[i]);
paint.setColorFilter(porterDuffColorFilter);
canvas.drawBitmap(dstBm, 0, 0, paint);
paint.setXfermode(null);
canvas.restoreToCount(sc);
// draw the label
canvas.drawText(labels[i], x + imgW / 2, y - labelP.getTextSize() / 2, labelP);
x += imgW + column_space;
// wrap around when we've drawn enough for one row
if ((i % column_num) == column_num - 1) {
x = 0;
y += imgH + row_space;
}
}
}
ColorMatrixColorFilter 滤镜
构造方法:
ColorMatrixColorFilter(float[] colorMatrix)
colorMatrix 颜色矩阵数组
使用:
// 原图效果
float[] colorMatrix = {
1,0,0,0,0, //red
0,1,0,0,0, //green
0,0,1,0,0, //blue
0,0,0,1,0 //alpha
};
// 红色加深效果
float[] colorMatrix = {
2,0,0,0,0, //red
0,1,0,0,0, //green
0,0,1,0,0, //blue
0,0,0,1,0 //alpha
};
// 灰白效果,就是让red=green=blue,这张图片来讲,0.4f的倍数就有些过度曝光了。1的时候全白了。
float[] colorMatrix = {
0.4f, 0.4f, 0.4f, 0, 0, //red
0.4f, 0.4f, 0.4f, 0, 0, //green
0.4f, 0.4f, 0.4f, 0, 0, //blue
0, 0, 0, 1, 0 //alpha
};
// 灰白效果,将亮度调小一点
float[] colorMatrix = {
0.3f, 0.3f, 0.3f, 0, 0, //red
0.3f, 0.3f, 0.3f, 0, 0, //green
0.3f, 0.3f, 0.3f, 0, 0, //blue
0, 0, 0, 1, 0 //alpha
};
mColorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix);
mPaint.setColorFilter(mColorMatrixColorFilter);
canvas.drawBitmap(mBitmap, 0, 0, mPaint);
其他效果:
// 黑白
public static final float colormatrix_heibai[] = {
0.8f, 1.6f, 0.2f, 0, -163.9f,
0.8f, 1.6f, 0.2f, 0, -163.9f,
0.8f, 1.6f, 0.2f, 0, -163.9f,
0, 0, 0, 1.0f, 0};
// 怀旧
public static final float colormatrix_huaijiu[] = {
0.2f, 0.5f, 0.1f, 0, 40.8f,
0.2f, 0.5f, 0.1f, 0, 40.8f,
0.2f, 0.5f, 0.1f, 0, 40.8f,
0, 0, 0, 1, 0};
// 哥特
public static final float colormatrix_gete[] = {
1.9f, -0.3f, -0.2f, 0, -87.0f,
-0.2f, 1.7f, -0.1f, 0, -87.0f,
-0.1f, -0.6f, 2.0f, 0, -87.0f,
0, 0, 0, 1.0f, 0};
// 淡雅
public static final float colormatrix_danya[] = {
0.6f, 0.3f, 0.1f, 0, 73.3f,
0.2f, 0.7f, 0.1f, 0, 73.3f,
0.2f, 0.3f, 0.4f, 0, 73.3f,
0, 0, 0, 1.0f, 0};
// 蓝调
public static final float colormatrix_landiao[] = {
2.1f, -1.4f, 0.6f, 0.0f, -71.0f,
-0.3f, 2.0f, -0.3f, 0.0f, -71.0f,
-1.1f, -0.2f, 2.6f, 0.0f, -71.0f,
0.0f, 0.0f, 0.0f, 1.0f, 0.0f};
// 光晕
public static final float colormatrix_guangyun[] = {
0.9f, 0, 0, 0, 64.9f,
0, 0.9f, 0, 0, 64.9f,
0, 0, 0.9f, 0, 64.9f,
0, 0, 0, 1.0f, 0};
// 梦幻
public static final float colormatrix_menghuan[] = {
0.8f, 0.3f, 0.1f, 0.0f, 46.5f,
0.1f, 0.9f, 0.0f, 0.0f, 46.5f,
0.1f, 0.3f, 0.7f, 0.0f, 46.5f,
0.0f, 0.0f, 0.0f, 1.0f, 0.0f};
// 酒红
public static final float colormatrix_jiuhong[] = {
1.2f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.9f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.8f, 0.0f, 0.0f,
0, 0, 0, 1.0f, 0};
// 反色 底片效果,所谓反色,就是将颜色置负,再加255偏移
public static final float colormatrix_fanse[] = {
-1.0f, 0.0f, 0.0f, 0.0f, 255.0f,
0.0f, -1.0f, 0.0f, 0.0f, 255.0f,
0.0f, 0.0f, -1.0f, 0.0f, 255.0f,
0.0f, 0.0f, 0.0f, 1.0f, 0.0f};
// 湖光掠影
public static final float colormatrix_huguang[] = {
0.8f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.9f, 0.0f, 0.0f,
0, 0, 0, 1.0f, 0};
// 褐片
public static final float colormatrix_hepian[] = {
1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.8f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.8f, 0.0f, 0.0f,
0, 0, 0, 1.0f, 0};
// 复古
public static final float colormatrix_fugu[] = {
0.9f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.8f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.5f, 0.0f, 0.0f,
0, 0, 0, 1.0f, 0};
// 泛黄
public static final float colormatrix_huan_huang[] = {
1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.5f, 0.0f, 0.0f,
0, 0, 0, 1.0f, 0};
// 传统
public static final float colormatrix_chuan_tong[] = {
1.0f, 0.0f, 0.0f, 0, -10f,
0.0f, 1.0f, 0.0f, 0, -10f,
0.0f, 0.0f, 1.0f, 0, -10f,
0, 0, 0, 1, 0};
// 胶片
public static final float colormatrix_jiao_pian[] = {
0.71f, 0.2f, 0.0f, 0.0f, 60.0f,
0.0f, 0.94f, 0.0f, 0.0f, 60.0f,
0.0f, 0.0f, 0.62f, 0.0f, 60.0f,
0, 0, 0, 1.0f, 0};
// 锐色
public static final float colormatrix_ruise[] = {
4.8f, -1.0f, -0.1f, 0, -388.4f,
-0.5f, 4.4f, -0.1f, 0, -388.4f,
-0.5f, -1.0f, 5.2f, 0, -388.4f,
0, 0, 0, 1.0f, 0};
// 清宁
public static final float colormatrix_qingning[] = {
0.9f, 0, 0, 0, 0,
0, 1.1f, 0, 0, 0,
0, 0, 0.9f, 0, 0,
0, 0, 0, 1.0f, 0};
// 浪漫
public static final float colormatrix_langman[] = {
0.9f, 0, 0, 0, 63.0f,
0, 0.9f, 0, 0, 63.0f,
0, 0, 0.9f, 0, 63.0f,
0, 0, 0, 1.0f, 0};
// 夜色
public static final float colormatrix_yese[] = {
1.0f, 0.0f, 0.0f, 0.0f, -66.6f,
0.0f, 1.1f, 0.0f, 0.0f, -66.6f,
0.0f, 0.0f, 1.0f, 0.0f, -66.6f,
0.0f, 0.0f, 0.0f, 1.0f, 0.0f};
将ColorMatrix替代颜色矩阵数组
// 亮度调节 绿色调亮
ColorMatrix cm = new ColorMatrix();
cm.setScale(1,2,1,1);
// 饱和度调节 0:无色彩 1:原图 2:饱和度加强
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
// 色调调节 第一个参数为0、1、2时代表绕红、绿、蓝轴旋转;第二个参数是旋转的角度。
ColorMatrix cm = new ColorMatrix();
cm.setRotate(2, 45);
mColorMatrixColorFilter = new ColorMatrixColorFilter(cm);
mPaint.setColorFilter(mColorMatrixColorFilter);
canvas.drawBitmap(mBitmap, 0, 0, mPaint);
private Paint mPaint; // 画笔
float phase;
PathEffect[] effects = new PathEffect[7];
int[] colors;
Path path;
private void init() {
mPaint = new Paint(); // 初始化
mPaint.setAntiAlias(true); // 抗锯齿
mPaint.setStyle(Paint.Style.STROKE); // 描边效果
path = new Path();
path.moveTo(0, 0);
for (int i=1; i<=40; i++) {
path.lineTo(i*40, (float) Math.random()*100);
}
path.close();
colors = new int[]{Color.BLACK, Color.BLUE, Color.CYAN, Color.GREEN,
Color.MAGENTA, Color.RED, Color.YELLOW};
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
effects[0] = null; // 原路径
effects[1] = new CornerPathEffect(10); // 在原路径基础上,拐角的地方成圆角。
effects[2] = new DiscretePathEffect(3.0f, 5.0f); // 在原路径基础上,进行路径的偏离。
effects[3] = new DashPathEffect(new float[]{20, 10, 5, 10}, phase); // 在原路径基础上,做虚线路径效果。
Path p = new Path();
p.addRect(0, 0, 3, 3, Path.Direction.CCW);
effects[4] = new PathDashPathEffect(p, 12, phase, PathDashPathEffect.Sty
effects[5] = new ComposePathEffect(effects[2], effects[4]);
effects[6] = new SumPathEffect(effects[2], effects[4]);
//将画布移动至(8,8)处开始绘制;
canvas.translate(8, 8);
for (int i = 0; i < effects.length; i++) {
mPaint.setPathEffect(effects[i]);
mPaint.setColor(colors[i]);
canvas.drawPath(path, mPaint);
canvas.translate(0, 130);
}
// 解除下面两行 图像就会动起来。
phase += 1;//改变phase的值,形成动画效果
invalidate();
}
注:DiscretePathEffect、DashPathEffect 这两个用到的比较多
CornerPathEffect 圆角路径效果
构造方法:
CornerPathEffect(float radius)
参数:radius 圆角半径
使用:
CornerPathEffect effects = new CornerPathEffect(10);
mPaint.setPathEffect(effects);
canvas.drawPath(path, mPaint);
DiscretePathEffect 偏离路径效果
构造方法:
DiscretePathEffect(float segmentLength, float deviation)
参数:segmentLength 线段长度,deviation 偏差
使用:
DiscretePathEffect effects = new DiscretePathEffect(3.0f, 5.0f);
mPaint.setPathEffect(effects);
canvas.drawPath(path, mPaint);
DashPathEffect 虚线路径效果
构造方法:
DashPathEffect(float intervals[], float phase)
参数:intervals间隔数组(数量必须为偶数个,且>=2,奇数下标的值表示绘制线的长,偶数下标的值表示间隔的长度),phase 偏移
使用:
DashPathEffect effects = new DashPathEffect(new float[]{20, 10, 5, 10}, phase);
mPaint.setPathEffect(effects);
canvas.drawPath(path, mPaint);
PathDashPathEffect 路径虚线路径效果
构造方法:
PathDashPathEffect(Path shape, float advance, float phase, Style style)
参数:shape 指定的形状,advance 每个形状之间的距离,phase 偏移,style 在填充时如何变换每个位置的形状
使用:
PathDashPathEffect effects =
new PathDashPathEffect(p, 12, phase, PathDashPathEffect.Style.MORPH);
mPaint.setPathEffect(effects);
canvas.drawPath(path, mPaint);
ComposePathEffect、SumPathEffect 组合路径效果
构造方法:
ComposePathEffect(PathEffect outerpe, PathEffect innerpe)
SumPathEffect(PathEffect first, PathEffect second)
使用:
ComposePathEffect effects =
new ComposePathEffect(new DiscretePathEffect(3.0f, 5.0f),
new PathDashPathEffect(p, 12, phase, PathDashPathEffect.Style.MORPH));
SumPathEffect effects =
new SumPathEffect(new DiscretePathEffect(3.0f, 5.0f),
new PathDashPathEffect(p, 12, phase, PathDashPathEffect.Style.MORPH));
mPaint.setPathEffect(effects);
canvas.drawPath(path, mPaint);