Shader 与LinearGradient 笔记

3 阅读1分钟

构造函数

public LinearGradient(float x0,float y0,float x1,float y1,int color0,int color2,TileMode tile)
//x0,y0开始位置 x1,y1结束位置,color0开始颜色,color1结束颜色,颜色值必须使用0xAARRGGBB透明度AA不能少。

pbulic LinearGradient(float x0,float y0,float x1,float y1,int colors[],int positions[],TItleMode tile)

//colors 用于填充的颜色数组,positions对应填充的位置

利用这个渐变可以实现一些彩色文字效果,示例:

int length = paint.measureText("hello world");
ValueAnimator animator = ValueAnimator.ofInt(02*length);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener{
        public void onAnimationUpdate(ValueAnimator animation) {
        mDx = (Integer) animation.getAnimatedValue();
        postInvalidate(); 
        }
})
animator.setRepeatMode(ValueAnimator.RESTART);

    LinearGradient lg = new LinearGradient(-length,0,0,0,int colors[],int pos,Shader.TileMode.CLAMP);
    
    Matrix matrix = new Matrix();
    matrix.setTranslate(mDx,0);
    lg.setLocalMatrix(matrix);
    paint.setShader(lg);