First, Setmaskfilter (Maskfilter maskfilter)
Setmaskfilter (Maskfilter Maskfilter) is a method in paint that can be used to perform certain processing of images. This method needs to pass in a Maskfilter object. But there is no implementation in the Maskfilter class, so we need to recognize its two subclasses, Blurmaskfilter and Embossmaskfilter, which are the Blur mask filter and the latter as the Emboss mask filter.
Second, Blurmaskfilter
2.1 Initial Knowledge
We explained in the previous cardview that CardView can set the shadow effect, and I'll take a picture of the previous article.
The above shadow effect can be easily achieved with blurmaskfilter.
@Override protectedvoid onDraw (canvas canvas) { super . OnDraw (canvas); Mpaint.setcolor (color.red); // Set the brush matte filter to pass in degrees and styles Mpaint.setmaskfilter (new blurmaskfilter (BlurMaskFilter.Blur.SOLID)); // Draw a rectangle Canvas.drawrect (n, A,mpaint); }
Description: This method has been labeled as obsolete, and if your app has hardware acceleration enabled, you won't see any shadow effects.
If hardware acceleration is turned off in the Androidmanifest.xml file, then our entire application will not support hardware acceleration, which is obviously unscientific, wouldn't it be nice if you could turn off hardware acceleration for only one view? Of course, Android also provides us with this feature, which we can pass in view:
Setlayertype (layer_type_software, NULL);
To turn off the hardware acceleration feature for individual view.
2.2 Constructors
Blurmaskfilter only one constructor with a parameter
Blurmaskfilter (float radius, blurmaskfilter.blur style)
First parameter: Radius is easy to understand, the larger the value, the more our shadows spread, the people who used PS would easily understand, in fact, the shadow range.
The second parameter: style represents the type of blur, which we use solid, the effect is that the alpha boundary of the image to produce a layer with the paint color consistent with the shadow effect without affecting the image itself, in addition to solid three, normal,outer and inner. Let's look at the effect one by one:
SOLID
The effect is to create a shadow effect that is consistent with the paint color outside the alpha boundary of the image without affecting the image itself.
Because the shadow color is removed from the color block, it is also red, so you feel not very comfortable, if the shadow is black is much better.
NORMAL
The entire image will be blurred out.
OUTER
Creates a layer of shading outside the alpha boundary and makes the original image transparent.
INNER
Creates a blur inside the image. You can see the shadow in the inside of the color block, there is a faint relief feeling.
Inner effect is not ideal, in practical applications we use less.
2.3 Adding a shadow to a picture
Blurmaskfilter is calculated based on the boundary of the alpha channel, and if you use it to manipulate the image, you will see no effect. So how do we add a shadow-like effect to a picture? Quite simply, we can try to get its alpha channel from bitmap and draw a blur effect on that alpha channel before drawing bitmap. The final effect is to overlay the shadow image below the original.
@Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); Bitmap Srcbitmap=Bitmapfactory.decoderesource (Mcontext.getresources (), R.drawable.kale); //get the alpha channel graph for a bitmapBitmap Shadowbitmap =Srcbitmap.extractalpha (); Mpaint.setcolor (Color.dkgray); //Set the brush matte filter to pass in degrees and stylesMpaint.setmaskfilter (NewBlurmaskfilter (10, BlurMaskFilter.Blur.NORMAL)); //Draw Shadows FirstCanvas.drawbitmap (Shadowbitmap, 200, 200, Mpaint); //painting the originalCanvas.drawbitmap (Srcbitmap, 200, 200,NULL); }
If you are curious, the bottom of the shadow picture is what it looks like, we don't draw the original image, just draw a look behind the shadow picture.
Third, Embossmaskfilter
3.1 Initial knowledge
Compared to the previous blurmaskfilter, the usability of the embossmaskfilter is relatively low because it does not have the most aggressive effect. As its name, he can achieve a similar relief effect, the white is to let you draw the image feels like "convex" from the screen more stereoscopic (similar effect in the design software is called bevel relief).
3.2 Constructors
Embossmaskfilter (float[] direction, float ambient, float specular, float Blurradius)
The various parameters in this understanding is more complex, if you want a detailed understanding can go to see: http://blog.csdn.net/aigestudio/article/details/41447349
I do not do anything here to explain, because to do the relief painting, looking for PS just fine.
Description: Most of this article is from: http://blog.csdn.net/aigestudio/article/details/41316141, I have a small deletion of the original text, recorded here as a learning note.
Detailed setmaskfilter of paint (Maskfilter maskfilter)