The Paint filter is implemented by modifying the color channel
Alpha filter processing
This is done using the paint.setMaskFilter () method
BlurMaskFilter
The BlurMaskFilter adds a blur effect to the painted area. Here’s how it is constructed:
public BlurMaskFilter(float radius, Blur style)
Copy the code
Radius indicates the size of the fuzzy radius. The larger the fuzzy radius is, the more fuzzy the styld indicates the fuzzy mode. There are four types:
- NORMAL: The entire drawing area will be blurred
- SOLID: The area around the drawing area will be blurred
- OUTER: The area around the drawing area will be blurred, and the drawing area will be empty
- INNER: The INNER boundaries of the drawing area are blurred
canvas.translate(20.20);
canvas.drawRect(rect, paint);
canvas.translate(0.200 * 1.5 f);
BlurMaskFilter blurMaskFilter = new BlurMaskFilter(20, BlurMaskFilter.Blur.NORMAL);
paint.setMaskFilter(blurMaskFilter);
canvas.drawRect(rect, paint);
canvas.translate(200 * 1.5 f.0);
blurMaskFilter = new BlurMaskFilter(50, BlurMaskFilter.Blur.NORMAL);
paint.setMaskFilter(blurMaskFilter);
canvas.drawRect(rect, paint);
canvas.translate(-200 * 1.5 f.200 * 1.5 f);
blurMaskFilter = new BlurMaskFilter(20, BlurMaskFilter.Blur.SOLID);
paint.setMaskFilter(blurMaskFilter);
canvas.drawRect(rect, paint);
canvas.translate(200 * 1.5 f.0);
blurMaskFilter = new BlurMaskFilter(20, BlurMaskFilter.Blur.OUTER);
paint.setMaskFilter(blurMaskFilter);
canvas.drawRect(rect, paint);
canvas.translate(200 * 1.5 f.0);
blurMaskFilter = new BlurMaskFilter(20, BlurMaskFilter.Blur.INNER);
paint.setMaskFilter(blurMaskFilter);
canvas.drawRect(rect, paint);
Copy the code
EmbossMaskFilter
To achieve an emboss effect, that is, the image is three-dimensional (which is not very good).
public EmbossMaskFilter(float[] direction, float ambient,
float specular, float blurRadius)
Copy the code
Direction indicates the location of the light source. The ambient indicates the intensity of the ambient light, which ranges from 0 to 1. Specular indicates the height of the light source, and blurRadius indicates the blurRadius
RGB color channel filter processing
Do this with paint.setColorFilter ()
ColorMatrixColorFilter
ColorMatrixColorFilter changes the pixel ARGB value of the drawn region by using a color matrix.
Matrix knowledge introduction
- define
Matrix multiplication3. Color matrix
Use a 4th order matrix to represent colorsSet the Alpha value to halfIf we want a color channel to increase by a certain amount, we cannot do this using a 4-order matrix and need to use a 5-order color matrix
A simple example
Bitmap bitmap = getBitmap();
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// A matrix with 4 rows and 5 columns
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.1 f.0
});
/ / set the ColorFilter
paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
canvas.translate(100.100);
// Draw the original image
canvas.drawBitmap(bitmap, 0.0.null);
canvas.translate(0, bitmap.getHeight() + 20);
// Draw the modified image
canvas.drawBitmap(bitmap, 0.0, paint);
Copy the code
The negative effect of
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
});
Copy the code
Black and white effect
Black and white effects use the principle of color removal: keep the RGB color values of the three channels the same, in order to keep the brightness constant R+G+B value of 1. The empirical values in Android are 0.213F, 0.715F, 0.072f
ColorMatrix colorMatrix = new ColorMatrix(new float[] {0.213 f.0.715 f.0.072 f.0.0.0.213 f.0.715 f.0.072 f.0.0.0.213 f.0.715 f.0.072 f.0.0.0.0.0.1.0
});
Copy the code
Color channel swap effect
/ / RGB into 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
});
Copy the code
Effect of restoring ancient ways
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
});
Copy the code
Color channel filtering effect
// Only red channels are reserved
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
});
Copy the code