|
| 1 | +package com.rae.cnblogs.widget; |
| 2 | + |
| 3 | +import android.animation.ValueAnimator; |
| 4 | +import android.content.Context; |
| 5 | +import android.graphics.Canvas; |
| 6 | +import android.graphics.Color; |
| 7 | +import android.graphics.Paint; |
| 8 | +import android.graphics.RectF; |
| 9 | +import android.util.AttributeSet; |
| 10 | +import android.util.TypedValue; |
| 11 | +import android.view.animation.LinearInterpolator; |
| 12 | + |
| 13 | +/** |
| 14 | + * 倒计时文本 |
| 15 | + * Created by ChenRui on 2017/10/31 0031 23:01. |
| 16 | + */ |
| 17 | +public class CountDownTextView extends RaeTextView { |
| 18 | + |
| 19 | + // 倒计时 |
| 20 | + private int duration = 5000; |
| 21 | + |
| 22 | + // 扫过的角度 |
| 23 | + private int mSweepAngle = 360; |
| 24 | + private ValueAnimator animator; |
| 25 | + private final RectF mRect = new RectF(); |
| 26 | + private Paint mBackgroundPaint; |
| 27 | + |
| 28 | + public CountDownTextView(Context context) { |
| 29 | + super(context); |
| 30 | + } |
| 31 | + |
| 32 | + public CountDownTextView(Context context, AttributeSet attrs) { |
| 33 | + super(context, attrs); |
| 34 | + } |
| 35 | + |
| 36 | + public CountDownTextView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 37 | + super(context, attrs, defStyleAttr); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + void init() { |
| 42 | + super.init(); |
| 43 | + mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 44 | + mBackgroundPaint.setColor(Color.WHITE); |
| 45 | + mBackgroundPaint.setStrokeWidth(dp2px(2)); |
| 46 | + mBackgroundPaint.setStyle(Paint.Style.STROKE); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * 开始倒计时 |
| 51 | + */ |
| 52 | + public void start() { |
| 53 | + // 在动画中 |
| 54 | + if (mSweepAngle != 360) return; |
| 55 | + |
| 56 | + mSweepAngle = 360; |
| 57 | + animator = ValueAnimator.ofInt(mSweepAngle).setDuration(duration); |
| 58 | + animator.setInterpolator(new LinearInterpolator()); |
| 59 | + animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 60 | + @Override |
| 61 | + public void onAnimationUpdate(ValueAnimator animation) { |
| 62 | + int value = (int) animation.getAnimatedValue(); |
| 63 | + mSweepAngle = 360 - value; |
| 64 | + invalidate(); |
| 65 | + } |
| 66 | + }); |
| 67 | + animator.start(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + protected void onDetachedFromWindow() { |
| 72 | + stop(); |
| 73 | + super.onDetachedFromWindow(); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + protected void onDraw(Canvas canvas) { |
| 78 | + int padding = dp2px(4); |
| 79 | + mRect.top = padding; |
| 80 | + mRect.left = padding; |
| 81 | + mRect.right = canvas.getWidth() - padding; |
| 82 | + mRect.bottom = canvas.getHeight() - padding; |
| 83 | + |
| 84 | + // 画倒计时线内圆 |
| 85 | + canvas.drawArc(mRect, //弧线所使用的矩形区域大小 |
| 86 | + 270, //开始角度 |
| 87 | + mSweepAngle, //扫过的角度 |
| 88 | + false, //是否使用中心 |
| 89 | + mBackgroundPaint); |
| 90 | + |
| 91 | + super.onDraw(canvas); |
| 92 | + } |
| 93 | + |
| 94 | + private int dp2px(int dp) { |
| 95 | + return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics()); |
| 96 | + } |
| 97 | + |
| 98 | + public void reset() { |
| 99 | + mSweepAngle = 360; |
| 100 | + animator = null; |
| 101 | + } |
| 102 | + |
| 103 | + public void stop() { |
| 104 | + if (animator != null) { |
| 105 | + animator.cancel(); |
| 106 | + animator = null; |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments