android textview 一个高亮效果

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Shader;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.content.ContextCompat;

public class ShineTextView extends AppCompatTextView {
    private LinearGradient gradient;
    private Matrix matrix;
    private float translate;
    private ValueAnimator animator;
    private int highlightColor; // 高亮颜色

    public ShineTextView(Context context) {
        super(context);
        init();
    }

    public ShineTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ShineTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        // 设置高亮颜色(这里使用更亮的白色,也可以自定义其他高亮色)
        highlightColor = Color.argb(255, 255, 255, 255); // 纯白

        // 创建线性渐变:白色 -> 高亮白 -> 白色
        gradient = new LinearGradient(0, 0, 200, 0,
                new int[]{
                        ContextCompat.getColor(getContext(), R.color.color_B3FFFFFF),    // 开始颜色
                        highlightColor, // 高亮颜色
                        ContextCompat.getColor(getContext(), R.color.color_B3FFFFFF)    // 结束颜色
                },
                new float[]{0, 0.5f, 1},
                Shader.TileMode.CLAMP);

        getPaint().setShader(gradient);
        matrix = new Matrix();

        // 创建动画
        animator = ValueAnimator.ofFloat(0, 1);
        animator.setDuration(1000);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setRepeatMode(ValueAnimator.RESTART);
        animator.addUpdateListener(animation -> {
            float value = (float) animation.getAnimatedValue();
            translate = getWidth() * value;
            postInvalidate();
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (matrix != null && gradient != null) {
            matrix.setTranslate(translate, 0);
            gradient.setLocalMatrix(matrix);
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (animator != null) {
            animator.start();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (animator != null) {
            animator.cancel();
        }
    }

    public void setHighlightColor(int color) {
        this.highlightColor = color;
        gradient = new LinearGradient(0, 0, 200, 0,
                new int[]{Color.WHITE, highlightColor, Color.WHITE},
                new float[]{0, 0.5f, 1}, Shader.TileMode.CLAMP);
        getPaint().setShader(gradient);
        invalidate();
    }
}

使用

        <ShineTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="loading...."
        android:textColor="#333333"
        android:textSize="40sp"
        android:textStyle="italic|bold" />

你可能感兴趣的:(android)