自定义控件之字体变色

@SuppressLint("AppCompatCustomView")
public class ColorTrackTextView extends TextView {
    //绘制不变字体的颜色
    private Paint originPaint;
    //改变后字体颜色
    private Paint changePaint;
    //当前进度
    private float currentProgress = 0.0f;
    private Direction direction = Direction.LEFT_TO_RIGHT;

    public enum Direction {
        LEFT_TO_RIGHT, RIGHT_TO_LEFT
    }

    public ColorTrackTextView(Context context) {
        this(context, null);
    }

    public ColorTrackTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
        initPaint(context, attrs);
    }

    public ColorTrackTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initPaint(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ColorTrackTextView);

        int originColor = array.getColor(R.styleable.ColorTrackTextView_originColor, getTextColors().getDefaultColor());
        int changeColor = array.getColor(R.styleable.ColorTrackTextView_changeColor, getTextColors().getDefaultColor());

        originPaint = getPaintByColor(originColor);
        changePaint = getPaintByColor(changeColor);

        // 回收
        array.recycle();
    }

    private Paint getPaintByColor(int color) {
        Paint paint = new Paint();
        // 设置颜色
        paint.setColor(color);
        // 设置抗锯齿
        paint.setAntiAlias(true);
        // 防抖动
        paint.setDither(true);
        // 设置字体的大小  就是TextView的字体大小
        paint.setTextSize(getTextSize());
        return paint;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int middle = (int) (currentProgress * getWidth());
        //从左变到右
        if (direction == Direction.LEFT_TO_RIGHT) {
            //绘制变色
            drawText(canvas, changePaint, 0, middle);
            drawText(canvas, originPaint, middle, getWidth());

        } else {
            //从右变到左
            drawText(canvas, changePaint, getWidth() - middle, getWidth());
            drawText(canvas, originPaint, 0, getWidth() - middle);

        }
    }

    private void drawText(Canvas canvas, Paint paint, int start, int end) {
        canvas.save();
        //绘制不变色
        Rect rect = new Rect(start, 0, end, getHeight());
        canvas.clipRect(rect);
        String text = getText().toString();
        Rect bound = new Rect();
        paint.getTextBounds(text, 0, text.length(), bound);
        //获取字体的宽度
        int x = getWidth() / 2 - bound.width() / 2;
        Paint.FontMetricsInt fontMetricsInt = paint.getFontMetricsInt();
        int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;
        System.out.println("dy:" + dy);
        int baseLine = getHeight() / 2 + dy;
        canvas.drawText(text, x, baseLine, paint);
        canvas.restore();

    }

    public void setOriginColor(int originPaint) {
        this.originPaint.setColor(originPaint);
    }

    public void setChangeColor(int changePaint) {
        this.changePaint.setColor(changePaint);
    }

    public void setCurrentProgress(float currentProgress) {
        this.currentProgress = currentProgress;
        invalidate();
    }

    public void setDirection(Direction direction) {
        this.direction = direction;
    }
}
main_activity核心代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:orientation="vertical"
    android:paddingTop="@dimen/activity_vertical_margin"
 >

    <com.cmj.textviewcolor.ColorTrackTextView
        android:id="@+id/color_track_tv"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:changeColor="@color/colorAccent"
        android:text="Hello World!" />

    <Button
        android:layout_width="wrap_content"
        android:text="左到右"
        android:onClick="leftToRight"
        android:layout_height="wrap_content" />


    <Button
        android:layout_width="wrap_content"
        android:text="右到左"
        android:onClick="rightToLeft"
        android:layout_height="wrap_content" />
</LinearLayout>
public void leftToRight(View view) {
    colorTrackTextView.setDirection(ColorTrackTextView.Direction.LEFT_TO_RIGHT);
    ValueAnimator valueAnimator = ObjectAnimator.ofFloat(0, 1);
    valueAnimator.setDuration(2000);
    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            float animatedValue = (float) valueAnimator.getAnimatedValue();
            colorTrackTextView.setCurrentProgress(animatedValue);
        }
    });
    valueAnimator.start();
}
public void rightToLeft(View view) {
    colorTrackTextView.setDirection(ColorTrackTextView.Direction.RIGHT_TO_LEFT);
    ValueAnimator valueAnimator = ObjectAnimator.ofFloat(0, 1);
    valueAnimator.setDuration(2000);
    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            float animatedValue = (float) valueAnimator.getAnimatedValue();
            colorTrackTextView.setCurrentProgress(animatedValue);
        }
    });
    valueAnimator.start();
}

猜你喜欢

转载自blog.csdn.net/qq_34015596/article/details/85459044