如上图所示闪闪发光字体:
HTextView 来源于
implementation 'com.hanks:htextview-base:0.1.2'
public class RainbowTextView extends HTextView { private Matrix mMatrix; private float mTranslate; private float colorSpeed; private float colorSpace; private int[] colors = {0XFF13C0FD, 0XFF11B2F7, 0XFF0D9BEE, 0XFF0984E6, 0XFF0879E1}; private LinearGradient mLinearGradient; public RainbowTextView(Context context) { this(context, null); } public RainbowTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public RainbowTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(attrs, defStyleAttr); } @Override public void setAnimationListener(AnimationListener listener) { throw new UnsupportedOperationException("Invalid operation for rainbow"); } private void init(AttributeSet attrs, int defStyleAttr) { TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.RainbowTextView); colorSpace = typedArray.getDimension(R.styleable.RainbowTextView_colorSpace, DisplayUtils.dp2px(150)); colorSpeed = typedArray.getDimension(R.styleable.RainbowTextView_colorSpeed, DisplayUtils.dp2px(5)); typedArray.recycle(); mMatrix = new Matrix(); initPaint(); } public float getColorSpace() { return colorSpace; } public void setColorSpace(float colorSpace) { this.colorSpace = colorSpace; } public float getColorSpeed() { return colorSpeed; } public void setColorSpeed(float colorSpeed) { this.colorSpeed = colorSpeed; } public void setColors(int... colors) { this.colors = colors; initPaint(); } private void initPaint() { int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); measure(w, h); int height = getMeasuredHeight(); mLinearGradient = new LinearGradient(0, 0, 0, height, colors, new float[]{0, 0.25f, 0.5f, 0.75f, 1.0f}, Shader.TileMode.MIRROR); getPaint().setShader(mLinearGradient); } @Override public void setProgress(float progress) { } @Override public void animateText(CharSequence text) { setText(text); } @Override protected void onDraw(Canvas canvas) { if (mMatrix == null) { mMatrix = new Matrix(); } mTranslate += colorSpeed; mMatrix.setTranslate(0, mTranslate); mLinearGradient.setLocalMatrix(mMatrix); super.onDraw(canvas); postInvalidateDelayed(100); }