Monday, November 9, 2015

android - How to apply deboss text effect

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="ABCDEFG"
        android:textSize="100dp"
        android:textStyle="bold"
        android:textColor="#8f9e92"
        android:fontFamily="sans-serif-condensed"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv"
        android:text="Deboss The Text"
        />
</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[]{ 0f, -1f, 0.5f }, // direction of the light source
                        0.8f, // ambient light between 0 to 1
                        13, // specular highlights
                        7.0f // blur before applying lighting
                );

                // Set the TextView layer type to software
                tv.setLayerType(View.LAYER_TYPE_SOFTWARE,null);

                // Finally, make the TextView text effect deboss.
                tv.getPaint().setMaskFilter(filter);
            }
        });
    }
}
More android examples