textView颜色渐变显示

public TextView invalidateTv(TextView tv, String s, int tv_size, String start_color, String end_color) {
    tv.setTextSize(tv_size);
    tv.setText(s);
    int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    tv.measure(spec, spec);
    int measuredWidthTicketNum = tv.getMeasuredWidth();
//核心代码
    LinearGradient mLinearGradient = new LinearGradient(0, 0, measuredWidthTicketNum, 0,
            Color.parseColor(start_color),
            Color.parseColor(end_color),
            Shader.TileMode.REPEAT);
    tv.getPaint().setShader(mLinearGradient);
    tv.invalidate();
    return tv;
}

一定要注意textView宽度的获取(之前一直没有效果,因为获取的宽度为零)

LinearGradient有两个构造函数;

public LinearGradient(float x0, float y0, float x1, float y1, int[] colors, float[] positions,Shader.TileMode tile)
float x0: 渐变起始点x坐标
float y0:渐变起始点y坐标
float x1:渐变结束点x坐标
float y1:渐变结束点y坐标
int[] colors:颜色 的int 数组
float[] positions: 相对位置的颜色数组,可为null,  若为null,可为null,颜色沿渐变线均匀分布
Shader.TileMode tile: 渲染器平铺模式


public LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1,Shader.TileMode tile)
float x0: 渐变起始点x坐标
float y0:渐变起始点y坐标
float x1:渐变结束点x坐标
float y1:渐变结束点y坐标
int color0: 起始渐变色
int color1: 结束渐变色
Shader.TileMode tile: 渲染器平铺模式

根据需求来选择用哪个方法。

转载于:https://my.oschina.net/u/3309455/blog/2995978

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值