Custom Controls (6): Filter Paint brushes by MaskFilter,

Source: Internet
Author: User
Tags drawtext

Custom Controls (6): Filter Paint brushes by MaskFilter,

 

First, let's look at an API: setMaskFilter (MaskFilter maskfilter ):

Set MaskFilter. Different maskfilters can be used to implement filter effects, such as filtering and stereo.

There are two child classes of MaskFilter to choose from:

BlurMaskFilter: Specifies a blur style and radius to process the Paint edge.

EmbossMaskFilter: Specifies the direction and ambient light intensity of the light source to add relief effect.

 

The following uses the Demo to see the effect:

1. BlurMaskFilter (Fuzzy effect)
Public class XBlurMaskFilterView extends View {public XBlurMaskFilterView (Context context) {super (context);} public XBlurMaskFilterView (Context context, AttributeSet attrs) {super (context, attrs );} public partition (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr);} @ Override protected void onDraw (Canvas canvas) {BlurMaskFilter bmf = null; paint paint = new Paint (); paint. setColor (Color. RED); // paint color. setStyle (Paint. style. FILL); // paint style paint. setAntiAlias (true); // anti-sawtooth paint. setStrokeWidth (4); // paint width. setTextSize (60); // draw the text size, in px // The first parameter is The Blur radius, and the second parameter is The Blur mode bmf = new BlurMaskFilter (4f, BlurMaskFilter. blur. NORMAL); paint. setMaskFilter (bmf); canvas. drawText ("this is The Blur effect", 100,100, paint); bmf = new BlurMaskFilter (4f, BlurMaskFilter. blur. OUTER); paint. setMaskFilter (bmf); canvas. drawText ("this is an external blur effect", 100,200, paint); bmf = new BlurMaskFilter (4f, BlurMaskFilter. blur. INNER); paint. setMaskFilter (bmf); canvas. drawText ("this is an internal blur effect", 100,300, paint); bmf = new BlurMaskFilter (4f, BlurMaskFilter. blur. SOLID); paint. setMaskFilter (bmf); canvas. drawText ("this is an internal bold, external blur effect", 100,400, paint); setLayerType (View. LAYER_TYPE_SOFTWARE, null); // disable hardware acceleration }}

 

:

 

Let's take a look at the core code:

new BlurMaskFilter(4f,BlurMaskFilter.Blur.NORMAL);
Two parameters: 1. Blur radius 2. Blur BlurMaskFilter. blur. NORMAL: internal and external fuzzy BlurMaskFilter. blur. OUTER: External fuzzy BlurMaskFilter. blur. INNER: Internal fuzzy BlurMaskFilter. blur. SOLID: Internal bold, external fuzzy

 

 

Ii. EmbossMaskFilter (embossed effect)

Public class XEmbossMaskFilterView extends View {public XEmbossMaskFilterView (Context context) {super (context);} public XEmbossMaskFilterView (Context context, AttributeSet attrs) {super (context, attrs );} public partition (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr);} @ Override protected void onDraw (Canvas canvas) {float [] direction = New float [] {1, 1, 3}; // set the direction of the light source float light = 0.4f; // set the environmental brightness float specular = 8; // define the mirror reflection coefficient float blur = 6.0f; // blur the radius EmbossMaskFilter emboss = new EmbossMaskFilter (direction, light, specular, blur); Paint paint = new Paint (); paint. setAntiAlias (true); // anti-sawtooth paint. setColor (Color. RED); // paint color. setStyle (Paint. style. FILL); // paint style paint. setTextSize (120); // draw the text size, in px paint. setStrokeWidth (14 );/ /Paint width: paint. setMaskFilter (emboss); paint. setMaskFilter (emboss); canvas. drawText ("this is a relief effect ~ ", 50,100, paint); setLayerType (View. LAYER_TYPE_SOFTWARE, null); // disable hardware acceleration }}

 

:

 

Core code:

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

Parameter meaning: direction: Floating Point array, used to control the direction of the x, y, and Z axes light source ambient: Set the ambient brightness, between 0 and 1 specular: mirror reflection coefficient blurRadius: blur radius

 

Note:

When using MaskFilter, you should note that when we use targetSdkVersion> = 14, MaskFilter will not be effective, because Android has enabled hardware acceleration by default in API 14 and later versions, this makes full use of GPU features to make painting smoother, but consumes more memory. So we turn off hardware acceleration. You can enable or disable hardware acceleration at different levels.

Application: Add android: hardwareAccelerated = "true" Activity to the application node of the configuration file: Add android: hardwareAccelerated = "false" View: you can call the View object after obtaining it, or directly set it in the onDraw () method of the View: view. setLayerType (View. LAYER_TYPE_HARDWARE, null );

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.