Android EmbossMaskFilter embossed effect implementation

Source: Internet
Author: User

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:

 

 

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.