Add/Set BitmapShader texture effect on textview text in android

How to create convert normal textview text to Bitmap shader text effect programmatically.

Bitmap shader is type of texture which draw as bitmap. So in this tutorial we are simply changing the normal textview text to bitmap shader text dynamically. Bitmap shader allows us to set any custom image or texture as only alphabet characters inside background. So here is the complete step by step tutorial for Add/Set BitmapShader texture effect on textview text in android.

android-project-download-code-button

Note : Please download the texture image and put inside drawable-hdpi folder.

drawable hdpi folder

 

Below image is the texture image, download this image and copy inside drawable-hdpi folder :

shader_background

How to Add/Set BitmapShader texture effect on textview text in android.

Code for MainActivity.java file.

 package com.bitmapshader_android_examples.com;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Shader;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

 TextView textview;
 Bitmap bitmap;
 Shader shader;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 textview = (TextView)findViewById(R.id.textView1);
 
 bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.shader_background);
 
 shader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
 
 textview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

 textview.getPaint().setShader(shader);
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.bitmapshader_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="TextView Text"
 android:textStyle="bold"
 android:gravity="center"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:textSize="100dp" />

</RelativeLayout>

Screenshot:

Add/Set BitmapShader texture effect on textview text in android

Click here to download Add/Set BitmapShader texture effect on textview text in android project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *