Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an image as textview's text background

Tags:

java

android

xml

Recently I've wanted to do something artsy with my new project like, as the title suggests, add a background image as a text's background, rather than the typical constant color all around. Specifically, I have a gradient image for this text.

Something like this

enter image description here

Typically I would expect an xml drawable resource file type solution, but any simple solution is greatly appreciated

EDIT:

Thanks for all the shader solutions, but for the sake of the question, only answers that provide a solution regarding image backgrounds will be accepted/given bounty

like image 939
Mazino Avatar asked Jan 05 '17 00:01

Mazino


4 Answers

You can apply a BitmapShader to the TextView paint.

myTextView.getPaint().setShader(new BitmapShader(myBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));

There are other types of shaders if you don't want to use an image. Documentation is here.

like image 125
James McCracken Avatar answered Nov 01 '22 01:11

James McCracken


Shader is the based class for objects that return horizontal spans of colors during drawing. A subclass of Shader is installed in a Paint calling paint.setShader(shader). After that any object (other than a bitmap) that is drawn with that paint will get its color(s) from the shader.

        Bitmap bitmapObj  = BitmapFactory.decodeResource(getResources(),R.drawable.your_image);
        Shader shaderObj = new BitmapShader(bitmapObj,Shader.TileMode.REPEAT,Shader.TileMode.REPEAT);
        textViewObj.getPaint().setShader(shaderObj);
        textViewObj.setText("hello");

You should call proper Shader.TileMode

You can also follow

  1. Gradient TextView
  2. Textview Gradient Text
like image 39
IntelliJ Amiya Avatar answered Nov 01 '22 00:11

IntelliJ Amiya


You can use yourTextView.getPaint().setShader(new LinearGradient(...));

There are two constructor for LinearGradient :

/** Create a shader that draws a linear gradient along a line.
    @param x0           The x-coordinate for the start of the gradient line
    @param y0           The y-coordinate for the start of the gradient line
    @param x1           The x-coordinate for the end of the gradient line
    @param y1           The y-coordinate for the end of the gradient line
    @param  colors      The colors to be distributed along the gradient line
    @param  positions   May be null. The relative positions [0..1] of
                                each corresponding color in the colors array. If this is null,
                                the the colors are distributed evenly along the gradient line.
    @param  tile        The Shader tiling mode
*/

public LinearGradient(float x0, float y0, float x1, float y1, int colors[], float positions[], TileMode tile)

And

/** Create a shader that draws a linear gradient along a line.
    @param x0       The x-coordinate for the start of the gradient line
    @param y0       The y-coordinate for the start of the gradient line
    @param x1       The x-coordinate for the end of the gradient line
    @param y1       The y-coordinate for the end of the gradient line
    @param  color0  The color at the start of the gradient line.
    @param  color1  The color at the end of the gradient line.
    @param  tile    The Shader tiling mode
*/
public LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1, TileMode tile)

Documentation of Shaders:

https://developer.android.com/reference/android/graphics/Shader.html

Documentation of Shaders.TileMode :

https://developer.android.com/reference/android/graphics/Shader.TileMode.html


Another way (as the idea and not tested):

Convert text to bitmap and drawable, then use GradientDrawable

like image 1
Misagh Avatar answered Nov 01 '22 01:11

Misagh


create gradient.xml in drawable folder. You can set the angle and start and end color. Set this drawable as the background

   <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="rectangle">
     <gradient
    android:startColor="#FFFFFF"
    android:endColor="#00000000"
    android:angle="45"/>    
    </shape>
like image 1
Ameer Avatar answered Nov 01 '22 00:11

Ameer