Text gradient android

Asked

Viewed 281 times

5

It is possible to create a gradient in the text (and not in the background) of a textView using only XML?

I know using Shader it is possible to do with code(here)

inserir a descrição da imagem aqui

It is possible to do this using only XML?

1 answer

2

It does not seem possible to create TextView with gradient text only using XML. However it is possible to achieve this effect by creating a canvas and drawing on it. It is necessary to declare our custom user interface element.

XML

<br.pacote.TextGradient
                android:id="@+id/txtVersao"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignRight="@+id/btnEntrar"
                android:gravity="right"
                android:textSize="40sp"
                android:text="GRADIENT" />

Textview Customizado

public class TextGradient extends TextView {

    public TextGradient(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public TextGradient(Context context) {
        super(context);
    }

    public TextGradient(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {

        Shader shader = new LinearGradient(0, 0, 0, super.getTextSize(), Color.RED, Color.BLUE,
                Shader.TileMode.CLAMP);
        super.getPaint().setShader(shader);
        super.setText(text.toString(), type);
    }
}

Imagery

inserir a descrição da imagem aqui

In this way, it is possible to use the TextGradient in the .xml.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.