Android EmbossMaskFilter embossed effect implementation
There seems to be no article about EmbossMaskFilter on the Internet, so I will study it here.
First, note that some people found on the internet that MaskFilter cannot be used, that is, some results cannot be displayed. correct this: Because android 4.0 and above enable hardware acceleration by default (you can use the code canvas. isHardwareAccelerated () to determine). Due to insufficient hardware acceleration, some effects cannot be displayed normally. how to disable the hardware acceleration Stamp: Click the open link
Now, let's go back to the topic. Let's take a look at the source code:
public class EmbossMaskFilter extends MaskFilter { /** * Create an emboss maskfilter * * @param direction array of 3 scalars [x, y, z] specifying the direction of the light source * @param ambient 0...1 amount of ambient light * @param specular coefficient for specular highlights (e.g. 8) * @param blurRadius amount to blur before applying lighting (e.g. 3) * @return the emboss maskfilter */ public EmbossMaskFilter(float[] direction, float ambient, float specular, float blurRadius) { if (direction.length < 3) { throw new ArrayIndexOutOfBoundsException(); } native_instance = nativeConstructor(direction, ambient, specular, blurRadius); } private static native int nativeConstructor(float[] direction, float ambient, float specular, float blurRadius);}
The source code is actually very simple. Create an embossed filter, that is, specify the direction of the light source and the ambient light intensity to add the embossed effect.
Let's see the parameter description:
Direction is a float array. It defines an array scalar [x, y, z] with a length of 3 to specify the direction of the light source.
The ambient value ranges from 0 to 1, and defines the background light or surrounding light.
Specular defines the mirror reflection coefficient.
BlurRadius blur radius.
The following code sets the effect
float []direction = new float[]{10, 10, 10};float ambient = 0.5f;float specular = 1;float blurRadius = 1;EmbossMaskFilter filter = new EmbossMaskFilter(direction, ambient, specular, blurRadius);paint.setMaskFilter(filter);Effect:
Modify the values of several parameters:
float ambient = 0.1f;float specular = 5;float blurRadius = 5;
Effect: