|
| 1 | +package cn.ypz.com.killetomrxmateria.rxwidget.loading; |
| 2 | + |
| 3 | +import android.animation.ValueAnimator; |
| 4 | +import android.content.Context; |
| 5 | +import android.graphics.Canvas; |
| 6 | +import android.graphics.Paint; |
| 7 | +import android.graphics.RectF; |
| 8 | +import android.support.annotation.Nullable; |
| 9 | +import android.util.AttributeSet; |
| 10 | +import android.util.Log; |
| 11 | +import android.view.animation.LinearInterpolator; |
| 12 | + |
| 13 | +import cn.ypz.com.killetomrxmateria.R; |
| 14 | +import cn.ypz.com.killetomrxmateria.rxwidget.RxDimenUtils; |
| 15 | +import cn.ypz.com.killetomrxmateria.rxwidget.base.BaseView; |
| 16 | + |
| 17 | +public class RxEasyArcLoading extends BaseView { |
| 18 | + private Paint arc0FP = new Paint(); |
| 19 | + private Paint arc1FP = new Paint(); |
| 20 | + private Paint arc2FP = new Paint(); |
| 21 | + private Paint arc3FP = new Paint(); |
| 22 | + private int dp50 = RxDimenUtils.dpToPx(50, getResources()); |
| 23 | + private int dp10 = RxDimenUtils.dpToPx(10, getResources()); |
| 24 | + private float multiple; |
| 25 | + private int arc0_f, arc1_f, arc2_f, arc3_f; |
| 26 | + private int arc0_s, arc1_s, arc2_s, arc3_s; |
| 27 | + private RectF oval; |
| 28 | + private int w; //View宽高 |
| 29 | + private int h; |
| 30 | + private float r; |
| 31 | + private int mini; |
| 32 | + |
| 33 | + private ValueAnimator arc0Animator = ValueAnimator.ofInt(0, 360); |
| 34 | + private ValueAnimator arc1Animator = ValueAnimator.ofInt(0, 360); |
| 35 | + private ValueAnimator arc2Animator = ValueAnimator.ofInt(0, 360); |
| 36 | + private ValueAnimator arc3Animator = ValueAnimator.ofInt(0, 360); |
| 37 | + |
| 38 | + public RxEasyArcLoading(Context context) { |
| 39 | + super(context); |
| 40 | + } |
| 41 | + |
| 42 | + public RxEasyArcLoading(Context context, @Nullable AttributeSet attrs) { |
| 43 | + super(context, attrs); |
| 44 | + paintSetColorId(arc0FP, R.color.deepskyblue); |
| 45 | + paintSetColorId(arc1FP, R.color.goldenrod); |
| 46 | + paintSetColorId(arc2FP, R.color.crimson); |
| 47 | + paintSetColorId(arc3FP, R.color.violet); |
| 48 | + reset(); |
| 49 | + arc1Animator.addUpdateListener(animation -> { |
| 50 | + arc1_f = (int) animation.getAnimatedValue() + 31; |
| 51 | + arc2_s = (int) animation.getAnimatedValue() - 127; |
| 52 | + }); |
| 53 | + arc2Animator.addUpdateListener(animation -> { |
| 54 | + arc2_f = (int) animation.getAnimatedValue() - 127; |
| 55 | + arc3_s = (int) animation.getAnimatedValue() + 100; |
| 56 | + }); |
| 57 | + arc3Animator.addUpdateListener(animation -> { |
| 58 | + arc3_f = (int) animation.getAnimatedValue() + 100; |
| 59 | + arc0_s = (int) animation.getAnimatedValue() - 26; |
| 60 | + }); |
| 61 | + arc0Animator.addUpdateListener(animation -> { |
| 62 | + arc0_f = (int) animation.getAnimatedValue() - 26; |
| 63 | + arc1_s = (int) animation.getAnimatedValue() + 31; |
| 64 | + postInvalidate(); |
| 65 | + }); |
| 66 | + arc0Animator.setDuration(3200); |
| 67 | + arc0Animator.setRepeatCount(-1); |
| 68 | + arc0Animator.setRepeatMode(ValueAnimator.RESTART); |
| 69 | + arc0Animator.setInterpolator(new LinearInterpolator()); |
| 70 | + arc1Animator.setDuration(2850); |
| 71 | + arc2Animator.setDuration(2300); |
| 72 | + arc3Animator.setDuration(1950); |
| 73 | + arc1Animator.setRepeatCount(-1); |
| 74 | + arc2Animator.setRepeatCount(-1); |
| 75 | + arc3Animator.setRepeatCount(-1); |
| 76 | + arc1Animator.setInterpolator(new LinearInterpolator()); |
| 77 | + arc2Animator.setInterpolator(new LinearInterpolator()); |
| 78 | + arc3Animator.setInterpolator(new LinearInterpolator()); |
| 79 | + oval = new RectF(); |
| 80 | + setPaint(arc0FP); |
| 81 | + setPaint(arc1FP); |
| 82 | + setPaint(arc2FP); |
| 83 | + setPaint(arc3FP); |
| 84 | + } |
| 85 | + |
| 86 | + private void setPaint(Paint paint) { |
| 87 | + paint.setAntiAlias(true);//使用抗锯齿功能 |
| 88 | + paint.setStyle(Paint.Style.STROKE);// |
| 89 | + if (multiple > 1) paint.setStrokeWidth(dp10 * multiple / 5); |
| 90 | + else paint.setStrokeWidth(dp10 / 4); |
| 91 | + } |
| 92 | + |
| 93 | + protected void reset() { |
| 94 | + arc0_s = arc0_f = -26; |
| 95 | + arc1_s = arc1_f = 31; |
| 96 | + arc2_s = arc2_f = -127; |
| 97 | + arc3_s = arc3_f = 100; |
| 98 | + } |
| 99 | + |
| 100 | + protected void stop() { |
| 101 | + if (arc0Animator.isRunning()) arc0Animator.pause(); |
| 102 | + if (arc1Animator.isRunning()) arc1Animator.pause(); |
| 103 | + if (arc2Animator.isRunning()) arc2Animator.pause(); |
| 104 | + if (arc3Animator.isRunning()) arc3Animator.pause(); |
| 105 | + } |
| 106 | + |
| 107 | + protected void reStart() { |
| 108 | + if (arc0Animator.isPaused()) arc0Animator.resume(); |
| 109 | + if (arc1Animator.isPaused()) arc0Animator.resume(); |
| 110 | + if (arc2Animator.isPaused()) arc0Animator.resume(); |
| 111 | + if (arc3Animator.isPaused()) arc0Animator.resume(); |
| 112 | + } |
| 113 | + |
| 114 | + public void cancle() { |
| 115 | + if (arc0Animator.isStarted()) arc0Animator.cancel(); |
| 116 | + if (arc1Animator.isStarted()) arc1Animator.cancel(); |
| 117 | + if (arc2Animator.isStarted()) arc2Animator.cancel(); |
| 118 | + if (arc3Animator.isStarted()) arc3Animator.cancel(); |
| 119 | + } |
| 120 | + |
| 121 | + public void start() { |
| 122 | + if (!arc0Animator.isStarted()) arc0Animator.start(); |
| 123 | + if (!arc1Animator.isStarted()) arc1Animator.start(); |
| 124 | + if (!arc2Animator.isStarted()) arc2Animator.start(); |
| 125 | + if (!arc3Animator.isStarted()) arc3Animator.start(); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 130 | + super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 131 | + Log.i("ypzLoading", widthMeasureSpec + "\n" + dp50); |
| 132 | + if (!isSetDimen(widthMeasureSpec) || widthMeasureSpec < dp50) { |
| 133 | + widthMeasureSpec = dp50; |
| 134 | + } |
| 135 | + if (!isSetDimen(heightMeasureSpec) || heightMeasureSpec < dp50) { |
| 136 | + heightMeasureSpec = dp50; |
| 137 | + } |
| 138 | + setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 143 | + super.onSizeChanged(w, h, oldw, oldh); |
| 144 | + this.w = w; |
| 145 | + this.h = h; |
| 146 | + mini = Math.min(w,h); |
| 147 | + multiple = Math.min(w, h) / dp50; |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + protected void initAttr(Context context, @Nullable AttributeSet attrs) { |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + protected void onDraw(Canvas canvas) { |
| 158 | + super.onDraw(canvas); |
| 159 | + canvas.translate(w / 2, h / 2);//绘画点移动至中心点 |
| 160 | + int i = 1; |
| 161 | + for (int nextR=mini/2-dp10;nextR>0; nextR-=dp10){ |
| 162 | + oval.set(-nextR, -nextR, nextR, nextR); |
| 163 | + if (i%2==0){ |
| 164 | + canvas.drawArc(oval, arc0_s, 52, false, arc0FP); |
| 165 | + canvas.drawArc(oval, arc1_s, 64, false, arc1FP); |
| 166 | + canvas.drawArc(oval, arc2_s, 96, false, arc2FP); |
| 167 | + canvas.drawArc(oval, arc3_s, 128, false, arc3FP); |
| 168 | + }else { |
| 169 | + canvas.drawArc(oval, arc0_f, 52, false, arc0FP); |
| 170 | + canvas.drawArc(oval, arc1_f, 64, false, arc1FP); |
| 171 | + canvas.drawArc(oval, arc2_f, 96, false, arc2FP); |
| 172 | + canvas.drawArc(oval, arc3_f, 128, false, arc3FP); |
| 173 | + } |
| 174 | + i+=1; |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments