|
| 1 | +package com.zyao89.zcustomview.loading; |
| 2 | + |
| 3 | +import android.animation.Animator; |
| 4 | +import android.animation.ValueAnimator; |
| 5 | +import android.content.Context; |
| 6 | +import android.graphics.Canvas; |
| 7 | +import android.graphics.Color; |
| 8 | +import android.graphics.Paint; |
| 9 | +import android.graphics.RectF; |
| 10 | +import android.util.AttributeSet; |
| 11 | +import android.view.View; |
| 12 | + |
| 13 | +/** |
| 14 | + * Created by zyao89 on 2017/3/16. |
| 15 | + |
| 16 | + * For more projects: https://github.com/zyao89 |
| 17 | + * My Blog: http://zyao89.me |
| 18 | + */ |
| 19 | +public class ClockLoading extends View implements Animator.AnimatorListener, ValueAnimator.AnimatorUpdateListener |
| 20 | +{ |
| 21 | + private Paint mPaint; |
| 22 | + private int mViewWidth; |
| 23 | + private int mViewHeight; |
| 24 | + private int mViewCenterX; |
| 25 | + private int mViewCenterY; |
| 26 | + private int mBigRadius; |
| 27 | + private int mInnerRadius; |
| 28 | + private RectF mInnerCircleRectF; |
| 29 | + private float mStartAngle; |
| 30 | + private float mEndAngle; |
| 31 | + private ValueAnimator mStartAnimator; |
| 32 | + private boolean mIsFirstState = true; |
| 33 | + |
| 34 | + public ClockLoading(Context context) |
| 35 | + { |
| 36 | + this(context, null); |
| 37 | + } |
| 38 | + |
| 39 | + public ClockLoading(Context context, AttributeSet attrs) |
| 40 | + { |
| 41 | + this(context, attrs, 0); |
| 42 | + } |
| 43 | + |
| 44 | + public ClockLoading(Context context, AttributeSet attrs, int defStyleAttr) |
| 45 | + { |
| 46 | + super(context, attrs, defStyleAttr); |
| 47 | + init(context, attrs); |
| 48 | + } |
| 49 | + |
| 50 | + private void init(Context context, AttributeSet attrs) |
| 51 | + { |
| 52 | + mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 53 | + mPaint.setColor(Color.BLACK); |
| 54 | + |
| 55 | + mStartAnimator = ValueAnimator.ofFloat(0, 359); |
| 56 | + mStartAnimator.setStartDelay(500); |
| 57 | + |
| 58 | + initValues(); |
| 59 | + } |
| 60 | + |
| 61 | + private void initValues() |
| 62 | + { |
| 63 | + mBigRadius = 50; |
| 64 | + mInnerRadius = mBigRadius - 10; |
| 65 | + mInnerCircleRectF = new RectF(); |
| 66 | + |
| 67 | + mStartAngle = 0; |
| 68 | + mEndAngle = 0; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + protected void onSizeChanged(int w, int h, int oldw, int oldh) |
| 73 | + { |
| 74 | + mViewWidth = w; |
| 75 | + mViewHeight = h; |
| 76 | + mViewCenterX = w / 2; |
| 77 | + mViewCenterY = h / 2; |
| 78 | + |
| 79 | + mInnerCircleRectF.set(mViewCenterX - mInnerRadius, mViewCenterY - mInnerRadius, mViewCenterX + mInnerRadius, mViewCenterY + mInnerRadius); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + protected void onAttachedToWindow() |
| 84 | + { |
| 85 | + super.onAttachedToWindow(); |
| 86 | + if (mStartAnimator != null) |
| 87 | + { |
| 88 | + mStartAnimator.setRepeatCount(ValueAnimator.INFINITE); |
| 89 | + mStartAnimator.setDuration(1000); |
| 90 | + mStartAnimator.addListener(this); |
| 91 | + mStartAnimator.addUpdateListener(this); |
| 92 | + mStartAnimator.start(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + protected void onDetachedFromWindow() |
| 98 | + { |
| 99 | + super.onDetachedFromWindow(); |
| 100 | + if (mStartAnimator != null) |
| 101 | + { |
| 102 | + mStartAnimator.setRepeatCount(0); |
| 103 | + mStartAnimator.setDuration(0); |
| 104 | + mStartAnimator.cancel(); |
| 105 | + mStartAnimator.removeAllUpdateListeners(); |
| 106 | + mStartAnimator.removeAllListeners(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + protected void onDraw(Canvas canvas) |
| 112 | + { |
| 113 | +// canvas.drawCircle(mViewCenterX, mViewCenterY ,50, mPaint); |
| 114 | + |
| 115 | + canvas.drawArc(mInnerCircleRectF, mStartAngle, mEndAngle - mStartAngle, true, mPaint); |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + @Override |
| 120 | + public void onAnimationStart(Animator animation) |
| 121 | + { |
| 122 | + mStartAngle = 0; |
| 123 | + mEndAngle = 0; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void onAnimationEnd(Animator animation) |
| 128 | + { |
| 129 | + mStartAngle = 0; |
| 130 | + mEndAngle = 0; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void onAnimationCancel(Animator animation) |
| 135 | + { |
| 136 | + mStartAngle = 0; |
| 137 | + mEndAngle = 0; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public void onAnimationRepeat(Animator animation) |
| 142 | + { |
| 143 | + mIsFirstState = !mIsFirstState; |
| 144 | + if (mIsFirstState) |
| 145 | + { |
| 146 | + mStartAngle = 0; |
| 147 | + mEndAngle = 0; |
| 148 | + } |
| 149 | + else |
| 150 | + { |
| 151 | + mStartAngle = 0; |
| 152 | + mEndAngle = 360; |
| 153 | + } |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public void onAnimationUpdate(ValueAnimator animation) |
| 159 | + { |
| 160 | + float value = (float) animation.getAnimatedValue(); |
| 161 | + |
| 162 | + if (mIsFirstState) |
| 163 | + { |
| 164 | + mEndAngle = value; |
| 165 | + } |
| 166 | + else |
| 167 | + { |
| 168 | + mStartAngle = value; |
| 169 | + } |
| 170 | + |
| 171 | + postInvalidate(); |
| 172 | + } |
| 173 | +} |
0 commit comments