Gradient Text

2015-11-16  本文已影响141人  youmu178

本文章翻译于:Styling Android

Shader myShader = new LinearGradient( 
    0, 0, 0, 100, 
    Color.WHITE, Color.BLACK, 
    Shader.TileMode.CLAMP );
textview.getPaint().setShader( myShader );
public class GradientTextView extends TextView
{
    public GradientTextView( Context context )
    {
        super( context, null, -1 );
    }
    public GradientTextView( Context context, 
        AttributeSet attrs )
    {
        super( context, attrs, -1 );
    }
    public GradientTextView( Context context, 
        AttributeSet attrs, int defStyle )
    {
        super( context, attrs, defStyle );
    }
 
    @Override
    protected void onLayout( boolean changed, 
        int left, int top, int right, int bottom )
    {
        super.onLayout( changed, left, top, right, bottom );
        if(changed)
        {
            getPaint().setShader( new LinearGradient( 
                0, 0, 0, getHeight(), 
                Color.WHITE, Color.BLACK, 
                Shader.TileMode.CLAMP ) );
        }
    }
}
图
这种配置可以用于应用不同种类的梯度(LinearGradient,RadialGradient, SweepGradient),用位图填充文本(BitmapShader),甚至结合着色器(ComposeShader).甚至可以创建自己的自定义Shader。
源码
上一篇 下一篇

猜你喜欢

热点阅读