Paint滤镜效果

1,238 阅读3分钟

Paint滤镜通过修改颜色通道来实现

Alpha滤镜处理

通过Paint.setMaskFilter()方法来实现

BlurMaskFilter

BlurMaskFilter为绘制区域添加模糊效果,下面来看看其构造方法:

public BlurMaskFilter(float radius, Blur style)

radius表示模糊半径大小,越大越模糊
styld表示模糊方式,有4种类型:

  • NORMAL:整个绘制区域都会被模糊
  • SOLID:绘制区域周围会被模糊
  • OUTER:绘制区域周围会被模糊,并且绘制区域会被清空
  • INNER:绘制区域内部边界处会被模糊
canvas.translate(20, 20);
canvas.drawRect(rect, paint);

canvas.translate(0, 200 * 1.5f);
BlurMaskFilter blurMaskFilter = new BlurMaskFilter(20, BlurMaskFilter.Blur.NORMAL);
paint.setMaskFilter(blurMaskFilter);
canvas.drawRect(rect, paint);

canvas.translate(200 * 1.5f, 0);
blurMaskFilter = new BlurMaskFilter(50, BlurMaskFilter.Blur.NORMAL);
paint.setMaskFilter(blurMaskFilter);
canvas.drawRect(rect, paint);

canvas.translate(-200 * 1.5f, 200 * 1.5f);
blurMaskFilter = new BlurMaskFilter(20, BlurMaskFilter.Blur.SOLID);
paint.setMaskFilter(blurMaskFilter);
canvas.drawRect(rect, paint);

canvas.translate(200 * 1.5f, 0);
blurMaskFilter = new BlurMaskFilter(20, BlurMaskFilter.Blur.OUTER);
paint.setMaskFilter(blurMaskFilter);
canvas.drawRect(rect, paint);

canvas.translate(200 * 1.5f, 0);
blurMaskFilter = new BlurMaskFilter(20, BlurMaskFilter.Blur.INNER);
paint.setMaskFilter(blurMaskFilter);
canvas.drawRect(rect, paint);

image.png

EmbossMaskFilter

可实现浮雕效果,也就是是图像立体起来(但是实际上效果并不是很好)
下面来看其构造函数:

public EmbossMaskFilter(float[] direction, float ambient, 
    float specular, float blurRadius)

direction表示光源位置使用[x,y,z]方向坐标轴表示(就好比打光师在哪个位置打光)
ambient表示环境光强度,范围为[0~1](就好比是白天拍的还是晚上拍的)
specular表示镜面高光系数
blurRadius表示模糊半径

RGB颜色通道滤镜处理

通过Paint.setColorFilter()来实现

ColorMatrixColorFilter

ColorMatrixColorFilter通过使用颜色矩阵来修改绘制区域的像素ARGB值。

矩阵相关知识简介

  1. 定义 image.png
  2. 矩阵乘法 image.png
  3. 颜色矩阵
    使用4阶矩阵表示颜色 image.png 将Alpha值设置为一半 image.png 如果我们希望某个颜色通道增加一定数量,则使用4阶矩阵无法实现,需要使用一个5阶颜色矩阵 image.png

简单示例

Bitmap bitmap = getBitmap();
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
//4行5列的矩阵
ColorMatrix colorMatrix = new ColorMatrix(new float[]{
        1, 0, 0, 0, 0,
        0, 1, 0, 0, 0,
        0, 0, 0, 0, 0,
        0, 0, 0, 0.1f, 0
});
//设置ColorFilter
paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

canvas.translate(100, 100);
//绘制原始图片
canvas.drawBitmap(bitmap, 0, 0, null);
canvas.translate(0, bitmap.getHeight() + 20);
//绘制修改后的图片
canvas.drawBitmap(bitmap, 0, 0, paint);

image.png

底片效果

ColorMatrix colorMatrix = new ColorMatrix(new float[]{
        -1, 0, 0, 0, 255,
        0, -1, 0, 0, 255,
        0, 0, -1, 0, 255,
        0, 0, 0, 1, 0
});

1639017987(1).png

黑白效果

黑白效果使用去色原理:保持RGB这3通道的颜色值一样,为了亮度保持不变R+G+B的值为1。 在Android中的经验值为 0.213f, 0.715f, 0.072f

ColorMatrix colorMatrix = 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
});

1639018074(1).png

颜色通道交换效果

//RGB变为GBA
ColorMatrix colorMatrix = new ColorMatrix(new float[]{
        0, 1, 0, 0, 0,
        0, 0, 1, 0, 0,
        1, 0, 0, 0, 0,
        0, 0, 0, 1, 0
});

1639018147(1).png

复古效果

ColorMatrix colorMatrix = 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
});

1639018213(1).png

颜色通道过滤效果

//只保留红色通道
ColorMatrix colorMatrix = new ColorMatrix(new float[]{
        1, 0, 0, 0, 0,
        0, 0, 0, 0, 0,
        0, 0, 0, 0, 0,
        0, 0, 0, 1, 0
});

1639018281(1).png