Android Basics Getting Started tutorial--8.3.3 Paint api--maskfilter (mask)

Source: Internet
Author: User
<span id="Label3"></p>Android Basics Getting Started tutorial--8.3.3 Paint api--maskfilter (mask)<p><p>Tags (space delimited): android basics Getting Started Tutorial</p></p>Introduction to this Section: <blockquote> <blockquote> <p>There is one way to get started with the Android Basics tutorial--8.3.1 the Paint method in the three drawing tools Class:<br><strong>setmaskfilter (maskfilter maskfilter)</strong>:<br>Set maskfilter, you can use different maskfilter to achieve the effect of filters, such as filtering, stereoscopic, etc.<br>Instead of using this maskfilter directly, we will use two of its subclasses:<br><strong>blurmaskfilter</strong>: Specifies a blurred style and radius to handle the edges of the Paint.<br><strong>embossmaskfilter</strong>: Specifies the direction and ambient light intensity of the light source to add the Emboss effect.<br>Let's write an example to try it out!<br>Official API Documentation: blurmaskfilter;embossmaskfilter;</p> </blockquote> </blockquote>1.BlurMaskFilter (blur Effect) <blockquote> <blockquote> <p>What to say about the filter stereo, who knows how, examples see the Truth:</p> </blockquote> </blockquote><p><p><strong>code example</strong> :</p></p><p><p><strong>Run</strong> :</p></p><p><p></p></p><p><p><strong>Implementation Code</strong> :</p></p> <blockquote> <blockquote> <p>Here we create a custom view, complete the drawing inside!</p> </blockquote> </blockquote><p><p><strong>Blurmaskfilterview.java</strong>:</p></p><pre class="prettyprint"><code class="language-Java hljs java"><span class="hljs-javadoc"><span class="hljs-javadoc">/** * Created by Jay on 2015/10/21 0021.</span> * *</span><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">blurmaskfilterview</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">View</span>{</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-title"><span class="hljs-title">Blurmaskfilterview</span></span>(context Context) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>(context); }<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-title"><span class="hljs-title">Blurmaskfilterview</span></span>(context context, AttributeSet Attrs) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>(context, attrs); }<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-title"><span class="hljs-title">Blurmaskfilterview</span></span>(context context, AttributeSet attrs,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Defstyleattr) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>(context, attrs, defstyleattr); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnDraw</span></span>(canvas Canvas) {blurmaskfilter BMF =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>; Paint paint=<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Paint (); Paint.setantialias (<span class="hljs-keyword"><span class="hljs-keyword">true</span></span>);<span class="hljs-comment"><span class="hljs-comment">//anti-aliasing</span></span>Paint.setcolor (color.red);<span class="hljs-comment"><span class="hljs-comment">//brush Color</span></span>Paint.setstyle (Paint.Style.FILL);<span class="hljs-comment"><span class="hljs-comment">//brush Style</span></span>Paint.settextsize (<span class="hljs-number"><span class="hljs-number"></span> the</span>);<span class="hljs-comment"><span class="hljs-comment">//draw text size, unit px</span></span>Paint.setstrokewidth (<span class="hljs-number"><span class="hljs-number">5</span></span>);<span class="hljs-comment"><span class="hljs-comment">//brush Thickness</span></span>BMF =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Blurmaskfilter (<span class="hljs-number"><span class="hljs-number">Ten</span></span>f,blurmaskfilter.blur.normal); Paint.setmaskfilter (bmf); Canvas.drawtext (<span class="hljs-string"><span class="hljs-string">"loved the Cao Shenzhi."</span></span>,<span class="hljs-number"><span class="hljs-number"></span> -</span>,<span class="hljs-number"><span class="hljs-number"></span> -</span>, paint); BMF =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Blurmaskfilter (<span class="hljs-number"><span class="hljs-number">Ten</span></span>f,blurmaskfilter.blur.outer); Paint.setmaskfilter (bmf); Canvas.drawtext (<span class="hljs-string"><span class="hljs-string">"loved the Cao Shenzhi."</span></span>,<span class="hljs-number"><span class="hljs-number"></span> -</span>,<span class="hljs-number"><span class="hljs-number"></span> $</span>, paint); BMF =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Blurmaskfilter (<span class="hljs-number"><span class="hljs-number">Ten</span></span>f,blurmaskfilter.blur.inner); Paint.setmaskfilter (bmf); Canvas.drawtext (<span class="hljs-string"><span class="hljs-string">"loved the Cao Shenzhi."</span></span>,<span class="hljs-number"><span class="hljs-number"></span> -</span>,<span class="hljs-number"><span class="hljs-number"></span> -</span>, paint); BMF =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Blurmaskfilter (<span class="hljs-number"><span class="hljs-number">Ten</span></span>f,blurmaskfilter.blur.solid); Paint.setmaskfilter (bmf); Canvas.drawtext (<span class="hljs-string"><span class="hljs-string">"loved the Cao Shenzhi."</span></span>,<span class="hljs-number"><span class="hljs-number"></span> -</span>,<span class="hljs-number"><span class="hljs-number"></span> -</span>, paint); Setlayertype (view.layer_type_software,<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>);<span class="hljs-comment"><span class="hljs-comment">//turn off hardware acceleration</span></span>}}</code></pre> <blockquote> <blockquote> <p>well, from the above code example, we can find that we use this blurmaskfilter, nothing more,<br>To instantiate in a construction method:<br><strong>Blurmaskfilter (10f,blurmaskfilter.blur.normal);</strong><br>The two parameters that we can control are:<br><strong>First parameter</strong> : Specifies the radius of the blurred edge;<br><strong>second parameter</strong> : Specifies the style of the blur, the optional Values are:<br>Blurmaskfilter.blur. <strong>NORMAL</strong>: internal and external Blur<br>Blurmaskfilter.blur. <strong>OUTER</strong>: External Blur<br>Blurmaskfilter.blur. <strong>INNER</strong>: Internal Blur<br>Blurmaskfilter.blur. <strong>SOLID</strong>: internal bold, external Blur</p> </blockquote> </blockquote><p><p>May be a little unclear, let's find a picture to try it out:</p></p><p><p></p></p><p><p>Here we change the blur radius to 50, it is more obvious ~</p></p>2.EmbossMaskFilter (embossed Effect) <blockquote> <blockquote> <p>title, by specifying the direction of the ambient light and the intensity of the ambient light to add the Emboss effect, Let's also write an example to see the effect:</p> </blockquote> </blockquote><p><p><strong>code example</strong> :</p></p><p><p><strong>Run</strong> :</p></p><p><p></p></p><p><p><strong>Implementation Code</strong> :</p></p><pre class="prettyprint"><code class="language-Java hljs java"><span class="hljs-javadoc"><span class="hljs-javadoc">/** * Created by Jay on 2015/10/22 0022.</span> * *</span><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">embossmaskfilterview</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">View</span>{</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-title"><span class="hljs-title">Embossmaskfilterview</span></span>(context Context) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>(context); }<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-title"><span class="hljs-title">Embossmaskfilterview</span></span>(context context, AttributeSet Attrs) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>(context, attrs); }<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-title"><span class="hljs-title">Embossmaskfilterview</span></span>(context context, AttributeSet attrs,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Defstyleattr) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>(context, attrs, defstyleattr); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnDraw</span></span>(canvas Canvas) {<span class="hljs-keyword"><span class="hljs-keyword">float</span></span>[] Direction =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span> <span class="hljs-keyword"><span class="hljs-keyword">float</span></span>[]{<span class="hljs-number"><span class="hljs-number">1</span></span>,<span class="hljs-number"><span class="hljs-number">1</span></span>,<span class="hljs-number"><span class="hljs-number">3</span></span>};<span class="hljs-comment"><span class="hljs-comment">//set the direction of the light source</span></span> <span class="hljs-keyword"><span class="hljs-keyword">float</span></span>Light =<span class="hljs-number"><span class="hljs-number">0.4</span></span>F<span class="hljs-comment"><span class="hljs-comment">//set ambient light level</span></span> <span class="hljs-keyword"><span class="hljs-keyword">float</span></span>Specular =<span class="hljs-number"><span class="hljs-number">8</span></span>;<span class="hljs-comment"><span class="hljs-comment">//define Specular reflection coefficient</span></span> <span class="hljs-keyword"><span class="hljs-keyword">float</span></span>Blur =<span class="hljs-number"><span class="hljs-number">3.0</span></span>F<span class="hljs-comment"><span class="hljs-comment">//blur radius</span></span>Embossmaskfilter emboss=<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Embossmaskfilter (direction,light,specular,blur); Paint paint =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Paint (); Paint.setantialias (<span class="hljs-keyword"><span class="hljs-keyword">true</span></span>);<span class="hljs-comment"><span class="hljs-comment">//anti-aliasing</span></span>Paint.setcolor (color.blue);<span class="hljs-comment"><span class="hljs-comment">//brush Color</span></span>Paint.setstyle (Paint.Style.FILL);<span class="hljs-comment"><span class="hljs-comment">//brush Style</span></span>Paint.settextsize (<span class="hljs-number"><span class="hljs-number"></span> -</span>);<span class="hljs-comment"><span class="hljs-comment">//draw text size, unit px</span></span>Paint.setstrokewidth (<span class="hljs-number"><span class="hljs-number">8</span></span>);<span class="hljs-comment"><span class="hljs-comment">//brush Thickness</span></span>Paint.setmaskfilter (emboss); Paint.setmaskfilter (emboss); Canvas.drawtext (<span class="hljs-string"><span class="hljs-string">"loved the Cao Shenzhi."</span></span>,<span class="hljs-number"><span class="hljs-number"></span> -</span>,<span class="hljs-number"><span class="hljs-number"></span> -</span>, paint); Bitmap Bitmap = Bitmapfactory.decoderesource (getresources (), r.mipmap.ic_bg_meizi1); Canvas.drawbitmap (bitmap,<span class="hljs-number"><span class="hljs-number"></span> max</span>,<span class="hljs-number"><span class="hljs-number"></span> $</span>, paint); Setlayertype (view.layer_type_software,<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>);<span class="hljs-comment"><span class="hljs-comment">//turn off hardware acceleration</span></span>}}</code></pre> <blockquote> <blockquote> <p>From we can see some embossmaskfilter effect, modify the light, form a relief effect ~ Sister figure is not obvious,<br>The text is very clear to show the texture! As with blurmaskfilter, the relevant settings are made in the construction method!<br><strong>embossmaskfilter (float[] direction, float ambient, float specular, float Blurradius)</strong><br>The parameters are in Turn:<br><strong>direction</strong>: Floating-point array for controlling the light direction of the x, y, Z axis<br><strong>Ambient</strong>: Set ambient brightness between 0 and 1<br><strong>Specular</strong>: specular reflection coefficient<br><strong>Blurradius</strong>: Blur radius<br>You can modify these values, try different effects, such as I modify the above, but also another effect:<br>Here for the obvious point, replaced by the green<br></p> </blockquote> </blockquote>3. Precautions <blockquote> <blockquote> <p>When using maskfilter, be aware that when our targetsdkversion >= 14, maskfilter<br>Will not work, this is because Android in the API version 14 and above are enabled by default hardware acceleration, so that the full<br>Using the features of the GPU makes painting smoother, but consumes more memory! All right, Let's get the hardware up and down.<br>ok, You can turn hardware acceleration on or off at different levels, usually off ~</p> <ul> <li><strong>Application</strong>: Add the Application node in the configuration file:<br>Android:hardwareaccelerated= "true"</li> <li><strong>activity</strong>: The Activity node in the configuration file is added<br>Android:hardwareaccelerated= "false"</li> <li><strong>View</strong>: Can be called after the view object, or directly in the View's OnDraw () method, set:<br>View.setlayertype (view.layer_type_hardware, null);</li> </ul> </blockquote> </blockquote>Sample Code Download:<p><p>Maskfilterdemo.zip</p></p>This section summarizes: <blockquote> <blockquote> <p>This section shows you an API for paint,<strong>setmaskfilter (maskfilter maskfilter)</strong>, to learn<br>Maskfilter basic usage of two subclasses: blurmaskfilter (blur Effect) and Embossmaskfilter (emboss effect),<br>Relatively simple, learn a little more, to our advanced part of the custom control is also no harm ~ well, say so much,<br>Thank you ~<br>By the way, I forgot to say that there is a class in example in the SDK that demonstrates both of these usages:<br><strong>Samples\android-xx\legacy\apidemos\src\com\example\android\apis\graphics</strong><br>Directory:<strong>Fingerpaint.java</strong> File ~</p> </blockquote> </blockquote> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android Basics Getting Started tutorial--8.3.3 Paint api--maskfilter (mask)</p></p></span>

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.