Monday, November 9, 2015

Android EmbossMaskFilter Example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ANDROID"
        android:textSize="100dp"
        android:textStyle="bold"
        android:textColor="#3c99f5"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv"
        android:text="Emboss Now"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcode;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.graphics.EmbossMaskFilter;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();

        // Get the widgets reference from XML layout
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        tv = (TextView) findViewById(R.id.tv);
        Button btn = (Button) findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                    MaskFilter
                        MaskFilter is the base class for object that perform transformations on an
                        alpha-channel mask before drawing it. A subclass of MaskFilter may be installed
                        into a Paint. Blur and emboss are implemented as subclasses of MaskFilter.
                */
                /*
                    public EmbossMaskFilter (float[] direction, float ambient, float specular,
                        float blurRadius)

                    Create an emboss maskfilter

                    Parameters
                        direction : array of 3 scalars [x, y, z] specifying the direction of the
                            light source
                        ambient : 0...1 amount of ambient light
                        specular : coefficient for specular highlights (e.g. 8)
                        blurRadius : amount to blur before applying lighting (e.g. 3)
                    Returns
                        the emboss maskfilter
                */
                EmbossMaskFilter filter = new EmbossMaskFilter(
                        new float[]{1,5,1}, // direction of the light source
                        0.5f, // ambient light between 0 to 1
                        10, // specular highlights
                        7.5f // blur before applying lighting
                );

                /*
                    public void setLayerType (int layerType, Paint paint)
                        Specifies the type of layer backing this view. The layer can be LAYER_TYPE_NONE,
                        LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE.

                        A layer is associated with an optional Paint instance that controls how the
                        layer is composed on screen.

                    Parameters
                        layerType : The type of layer to use with this view, must be one of
                            LAYER_TYPE_NONE, LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE
                        paint : The paint used to compose the layer. This argument is optional and
                            can be null. It is ignored when the layer type is LAYER_TYPE_NONE
                */
                /*
                    public static final int LAYER_TYPE_SOFTWARE
                        Indicates that the view has a software layer. A software layer is backed by
                        a bitmap and causes the view to be rendered using Android's software rendering
                        pipeline, even if hardware acceleration is enabled.
                */
                tv.setLayerType(View.LAYER_TYPE_SOFTWARE,null);

                /*
                    public MaskFilter setMaskFilter (MaskFilter maskfilter)

                        Set or clear the maskfilter object.
                        Pass null to clear any previous maskfilter. As a convenience, the parameter
                        passed is also returned.

                    Parameters
                        maskfilter : May be null. The maskfilter to be installed in the paint
                    Returns
                        maskfilter
                */
                // Finally, make the TextView text embossed.
                tv.getPaint().setMaskFilter(filter);
            }
        });
    }
}
More android examples