|
|
@@ -12,23 +12,27 @@ import android.util.AttributeSet;
|
|
|
import android.view.View;
|
|
|
import android.view.animation.LinearInterpolator;
|
|
|
|
|
|
-// todo 内存泄露
|
|
|
-public class LVCircularRing extends View {
|
|
|
+import java.lang.ref.WeakReference;
|
|
|
|
|
|
+/**
|
|
|
+ * 自定义加载动画
|
|
|
+ * @author zc
|
|
|
+ */
|
|
|
+public class CircularRingView extends View {
|
|
|
private float mWidth = 0f;
|
|
|
private float mPadding = 0f;
|
|
|
private float startAngle = 0f;
|
|
|
private Paint mPaint;
|
|
|
|
|
|
- public LVCircularRing(Context context) {
|
|
|
+ public CircularRingView(Context context) {
|
|
|
this(context, null);
|
|
|
}
|
|
|
|
|
|
- public LVCircularRing(Context context, AttributeSet attrs) {
|
|
|
+ public CircularRingView(Context context, AttributeSet attrs) {
|
|
|
this(context, attrs, 0);
|
|
|
}
|
|
|
|
|
|
- public LVCircularRing(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
+ public CircularRingView(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
super(context, attrs, defStyleAttr);
|
|
|
initPaint();
|
|
|
}
|
|
|
@@ -51,8 +55,8 @@ public class LVCircularRing extends View {
|
|
|
canvas.drawCircle(mWidth / 2, mWidth / 2, mWidth / 2 - mPadding, mPaint);
|
|
|
mPaint.setColor(Color.WHITE);
|
|
|
RectF rectF = new RectF(mPadding, mPadding, mWidth - mPadding, mWidth - mPadding);
|
|
|
- canvas.drawArc(rectF, startAngle, 100
|
|
|
- , false, mPaint);//第四个参数是否显示半径
|
|
|
+ //第四个参数是否显示半径
|
|
|
+ canvas.drawArc(rectF, startAngle, 100, false, mPaint);
|
|
|
|
|
|
}
|
|
|
|
|
|
@@ -75,39 +79,52 @@ public class LVCircularRing extends View {
|
|
|
valueAnimator.setRepeatCount(1);
|
|
|
valueAnimator.cancel();
|
|
|
valueAnimator.end();
|
|
|
+ valueAnimator.removeAllUpdateListeners();
|
|
|
+ valueAnimator.removeAllListeners();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- ValueAnimator valueAnimator;
|
|
|
-
|
|
|
+ private ValueAnimator valueAnimator;
|
|
|
private ValueAnimator startViewAnim(float startF, final float endF, long time) {
|
|
|
+ if (valueAnimator != null && valueAnimator.isRunning()) {
|
|
|
+ return valueAnimator;
|
|
|
+ }
|
|
|
+
|
|
|
valueAnimator = ValueAnimator.ofFloat(startF, endF);
|
|
|
valueAnimator.setDuration(time);
|
|
|
valueAnimator.setInterpolator(new LinearInterpolator());
|
|
|
- valueAnimator.setRepeatCount(ValueAnimator.INFINITE);//无限循环
|
|
|
- valueAnimator.setRepeatMode(ValueAnimator.RESTART);//
|
|
|
-
|
|
|
- valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
|
|
|
- @Override
|
|
|
- public void onAnimationUpdate(ValueAnimator valueAnimator) {
|
|
|
- float value = (float) valueAnimator.getAnimatedValue();
|
|
|
- startAngle = 360 * value;
|
|
|
- invalidate();
|
|
|
- }
|
|
|
- });
|
|
|
+ //无限循环
|
|
|
+ valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
|
|
|
+ valueAnimator.setRepeatMode(ValueAnimator.RESTART);
|
|
|
+ if (mUpdateListener == null) {
|
|
|
+ mUpdateListener = new UpdateListener(this);
|
|
|
+ }
|
|
|
+ valueAnimator.addUpdateListener(mUpdateListener);
|
|
|
+ valueAnimator.start();
|
|
|
+ return valueAnimator;
|
|
|
+ }
|
|
|
+
|
|
|
+ private UpdateListener mUpdateListener;
|
|
|
+ private static class UpdateListener implements ValueAnimator.AnimatorUpdateListener {
|
|
|
+ private WeakReference<CircularRingView> mView;
|
|
|
+ public UpdateListener(CircularRingView view) {
|
|
|
+ mView = new WeakReference<>(view);
|
|
|
+ }
|
|
|
|
|
|
- valueAnimator.addListener(new AnimatorListenerAdapter(){
|
|
|
- @Override
|
|
|
- public void onAnimationEnd(Animator animation) {
|
|
|
- super.onAnimationEnd(animation);
|
|
|
+ @Override
|
|
|
+ public void onAnimationUpdate(ValueAnimator animation) {
|
|
|
+ if (mView == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ CircularRingView view = mView.get();
|
|
|
+ if (view == null) {
|
|
|
+ return;
|
|
|
}
|
|
|
- });
|
|
|
|
|
|
- if (!valueAnimator.isRunning()) {
|
|
|
- valueAnimator.start();
|
|
|
+ float value = (float) view.valueAnimator.getAnimatedValue();
|
|
|
+ view.startAngle = 360 * value;
|
|
|
+ view.invalidate();
|
|
|
}
|
|
|
-
|
|
|
- return valueAnimator;
|
|
|
}
|
|
|
}
|
|
|
|