shader(LinearGradient线性渲染器)仿今日头条加载刷新

先看看效果效果图这里写图片描述

仔细对比之后发现和今日头条的有点不一样,
第一字体是向右倾斜的

  //设置字体向右倾斜
  paint.setTextSkewX(0.25f);

更改后的

这里写图片描述

    <com.example.administrator.meterialdesignpaintdemo.GridentText
        android:text="你关心的才是头条"
        android:textSize="40sp"
        android:textColor="@color/colorPrimaryDark"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />"

 public class GridentText extends AppCompatTextView {

    private LinearGradient gradient;
    private float charwidth;
    private Matrix matric;
    private float totalWidth;

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

    public GridentText(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TextPaint paint = getPaint();
        //计算每一个字符的宽度
        CharSequence text = getText();
        totalWidth = paint.measureText(text.toString());
        charwidth = totalWidth / text.length();
        int[] color = new int[]{Color.GRAY, Color.WHITE, Color.GRAY};
        gradient = new LinearGradient(0, 0, charwidth*2, charwidth, color, null, Shader.TileMode.CLAMP);
        paint.setShader(gradient);
        //设置字体向右倾斜
        paint.setTextSkewX(0.25f);
        matric = new Matrix();
    }


    float offset = 0;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //这里不断移动matrix的位置
        if (offset <= totalWidth) {
            matric.setTranslate(offset, 0);
            gradient.setLocalMatrix(matric);
            postInvalidate();
            offset += 10;
        }
    }
}

面用到了三阶矩阵Matrix,矩阵变换的功能很强大,比如再里面添加缩放,旋转等,待以后探索。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值