Android闪闪发光字体
生活随笔
收集整理的這篇文章主要介紹了
Android闪闪发光字体
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
如上圖所示閃閃發(fā)光字體:
HTextView 來(lái)源于?
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); }總結(jié)
以上是生活随笔為你收集整理的Android闪闪发光字体的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。