|
1 | 1 | package com.allen.androidcustomview.widget; |
2 | 2 |
|
3 | | -import android.animation.Animator; |
4 | | -import android.animation.AnimatorSet; |
5 | | -import android.animation.ValueAnimator; |
| 3 | + |
6 | 4 | import android.content.Context; |
7 | 5 | import android.graphics.Canvas; |
8 | 6 | import android.graphics.Color; |
@@ -28,27 +26,27 @@ public class LoadingButton extends Button { |
28 | 26 |
|
29 | 27 | private Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
30 | 28 |
|
31 | | - private Paint circlePaint1 = new Paint(Paint.ANTI_ALIAS_FLAG); |
32 | | - private Paint circlePaint2 = new Paint(Paint.ANTI_ALIAS_FLAG); |
33 | | - private Paint circlePaint3 = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 29 | + private Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 30 | + |
| 31 | + |
| 32 | + private static final int POINT_COLOR_1 = 0x4CFFFFFF; |
| 33 | + private static final int POINT_COLOR_2 = 0x7FFFFFFF; |
| 34 | + private static final int POINT_COLOR_3 = 0xFFFFFFFF; |
34 | 35 |
|
35 | | - private int circleColor = 0xffffff; |
36 | | - // private int circleColor = 0xf66b12; |
37 | | - private int duration = 1000; |
| 36 | + private int duration = 300; |
38 | 37 |
|
39 | 38 | private float centerX; |
40 | 39 | private float centerY; |
41 | 40 |
|
42 | 41 | private float radius; |
43 | 42 |
|
44 | | - private AnimatorSet animatorSet; |
45 | | - private ValueAnimator animator1; |
46 | | - private ValueAnimator animator2; |
47 | | - private ValueAnimator animator3; |
48 | | - |
49 | 43 |
|
50 | 44 | private boolean isLoading = false; |
51 | 45 |
|
| 46 | + private int mLoadingIndex = 0; |
| 47 | + private Runnable mRunnable; |
| 48 | + |
| 49 | + |
52 | 50 | public LoadingButton(Context context) { |
53 | 51 | this(context, null); |
54 | 52 | } |
@@ -58,84 +56,18 @@ public LoadingButton(Context context, @Nullable AttributeSet attrs) { |
58 | 56 |
|
59 | 57 | initPaint(); |
60 | 58 |
|
61 | | - animatorSet = new AnimatorSet(); |
62 | | - circleAnimator1(); |
63 | | - circleAnimator2(); |
64 | | - circleAnimator3(); |
65 | | - |
66 | | - animatorSet.addListener(new Animator.AnimatorListener() { |
67 | | - @Override |
68 | | - public void onAnimationStart(Animator animation) { |
69 | | - |
70 | | - } |
71 | | - |
72 | | - @Override |
73 | | - public void onAnimationEnd(Animator animation) { |
74 | | - animatorSet.start(); |
75 | | - } |
76 | | - |
77 | | - @Override |
78 | | - public void onAnimationCancel(Animator animation) { |
79 | | - |
80 | | - } |
81 | | - |
82 | | - @Override |
83 | | - public void onAnimationRepeat(Animator animation) { |
84 | | - |
85 | | - } |
86 | | - }); |
87 | | - animatorSet.play(animator1).with(animator2).with(animator3); |
88 | | - |
89 | | - } |
90 | | - |
91 | | - private void circleAnimator1() { |
92 | | - animator1 = ValueAnimator.ofInt(255, 76, 165); |
93 | | - animator1.setDuration(duration); |
94 | | - animator1.setRepeatCount(ValueAnimator.INFINITE); |
95 | | - animator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
96 | | - @Override |
97 | | - public void onAnimationUpdate(ValueAnimator animation) { |
98 | | - circlePaint1.setAlpha((Integer) animation.getAnimatedValue()); |
99 | | - invalidate(); |
100 | | - } |
101 | | - }); |
102 | 59 | } |
103 | 60 |
|
104 | | - private void circleAnimator2() { |
105 | | - animator2 = ValueAnimator.ofInt(165, 255, 76); |
106 | | - animator2.setDuration(duration); |
107 | | - animator2.setRepeatCount(ValueAnimator.INFINITE); |
108 | | - animator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
109 | | - @Override |
110 | | - public void onAnimationUpdate(ValueAnimator animation) { |
111 | | - circlePaint2.setAlpha((Integer) animation.getAnimatedValue()); |
112 | | - invalidate(); |
113 | | - } |
114 | | - }); |
115 | | - } |
116 | 61 |
|
117 | | - private void circleAnimator3() { |
118 | | - animator3 = ValueAnimator.ofInt(76, 165, 255); |
119 | | - animator3.setDuration(duration); |
120 | | - animator3.setRepeatCount(ValueAnimator.INFINITE); |
121 | | - animator3.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 62 | + private void initPaint() { |
| 63 | + initTextPaint(); |
| 64 | + circlePaint = getPaint(dp2px(1), Paint.Style.FILL); |
| 65 | + mRunnable = new Runnable() { |
122 | 66 | @Override |
123 | | - public void onAnimationUpdate(ValueAnimator animation) { |
124 | | - circlePaint3.setAlpha((Integer) animation.getAnimatedValue()); |
| 67 | + public void run() { |
125 | 68 | invalidate(); |
126 | 69 | } |
127 | | - }); |
128 | | - } |
129 | | - |
130 | | - private void initPaint() { |
131 | | - initTextPaint(); |
132 | | - |
133 | | - circlePaint1 = getPaint(dp2px(1), circleColor, Paint.Style.FILL); |
134 | | - circlePaint1.setAlpha(255); |
135 | | - circlePaint2 = getPaint(dp2px(1), circleColor, Paint.Style.FILL); |
136 | | - circlePaint2.setAlpha(165); |
137 | | - circlePaint3 = getPaint(dp2px(1), circleColor, Paint.Style.FILL); |
138 | | - circlePaint3.setAlpha(76); |
| 70 | + }; |
139 | 71 | } |
140 | 72 |
|
141 | 73 | /** |
@@ -163,29 +95,57 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
163 | 95 | centerY = mHeight / 2; |
164 | 96 |
|
165 | 97 | radius = mHeight / 8; |
166 | | - Log.d("allen", "onMeasure: "+centerX); |
| 98 | + Log.d("allen", "onMeasure: " + centerX); |
167 | 99 | } |
168 | 100 |
|
169 | 101 |
|
170 | 102 | @Override |
171 | 103 | protected void onDraw(Canvas canvas) { |
172 | 104 |
|
173 | 105 | if (isLoading) { |
174 | | - drawLoading(canvas); |
| 106 | + drawLoading(canvas, mLoadingIndex); |
| 107 | + mLoadingIndex = (mLoadingIndex + 1) % 3; |
| 108 | + postDelayed(mRunnable, duration); |
| 109 | + |
175 | 110 | } else { |
176 | 111 | super.onDraw(canvas); |
177 | 112 | } |
178 | 113 |
|
179 | 114 | } |
180 | 115 |
|
181 | | - private void drawLoading(Canvas canvas) { |
182 | | - canvas.drawCircle(centerX - radius * 4, centerY, radius, circlePaint1); |
183 | | - |
184 | | - canvas.drawCircle(centerX, centerY, radius, circlePaint2); |
| 116 | + private void drawLoading(Canvas canvas, int index) { |
| 117 | + if (index < 0 || index > 2) |
| 118 | + return; |
| 119 | + switch (index) { |
| 120 | + case 0: |
| 121 | + circlePaint.setColor(POINT_COLOR_1); |
| 122 | + canvas.drawCircle(centerX - radius * 4, centerY, radius, circlePaint); |
| 123 | + circlePaint.setColor(POINT_COLOR_2); |
| 124 | + canvas.drawCircle(centerX, centerY, radius, circlePaint); |
| 125 | + circlePaint.setColor(POINT_COLOR_3); |
| 126 | + canvas.drawCircle(centerX + radius * 4, centerY, radius, circlePaint); |
| 127 | + break; |
| 128 | + case 1: |
| 129 | + circlePaint.setColor(POINT_COLOR_3); |
| 130 | + canvas.drawCircle(centerX - radius * 4, centerY, radius, circlePaint); |
| 131 | + circlePaint.setColor(POINT_COLOR_1); |
| 132 | + canvas.drawCircle(centerX, centerY, radius, circlePaint); |
| 133 | + circlePaint.setColor(POINT_COLOR_2); |
| 134 | + canvas.drawCircle(centerX + radius * 4, centerY, radius, circlePaint); |
| 135 | + break; |
| 136 | + case 2: |
| 137 | + circlePaint.setColor(POINT_COLOR_2); |
| 138 | + canvas.drawCircle(centerX - radius * 4, centerY, radius, circlePaint); |
| 139 | + circlePaint.setColor(POINT_COLOR_3); |
| 140 | + canvas.drawCircle(centerX, centerY, radius, circlePaint); |
| 141 | + circlePaint.setColor(POINT_COLOR_1); |
| 142 | + canvas.drawCircle(centerX + radius * 4, centerY, radius, circlePaint); |
| 143 | + break; |
| 144 | + } |
185 | 145 |
|
186 | | - canvas.drawCircle(centerX + radius * 4, centerY, radius, circlePaint3); |
187 | 146 | } |
188 | 147 |
|
| 148 | + |
189 | 149 | /** |
190 | 150 | * 绘制文字 |
191 | 151 | * |
@@ -201,32 +161,33 @@ private void drawText(Canvas canvas, String textString) { |
201 | 161 |
|
202 | 162 |
|
203 | 163 | public void startLoading() { |
204 | | - if (animatorSet != null) { |
205 | | - isLoading = true; |
206 | | - animatorSet.start(); |
207 | | - } |
| 164 | + if (isLoading) |
| 165 | + return; |
| 166 | + isLoading = true; |
| 167 | + mLoadingIndex = 0; |
| 168 | + invalidate(); |
208 | 169 | } |
209 | 170 |
|
210 | 171 | public void stopLoading() { |
211 | | - if (animatorSet != null) { |
212 | | - isLoading = false; |
213 | | - animatorSet.end(); |
214 | | - postInvalidate(); |
215 | | - } |
| 172 | + if (!isLoading) |
| 173 | + return; |
| 174 | + isLoading = false; |
| 175 | + } |
| 176 | + |
| 177 | + public boolean isLoading() { |
| 178 | + return isLoading; |
216 | 179 | } |
217 | 180 |
|
218 | 181 | /** |
219 | 182 | * 统一处理paint |
220 | 183 | * |
221 | 184 | * @param strokeWidth |
222 | | - * @param color |
223 | 185 | * @param style |
224 | 186 | * @return |
225 | 187 | */ |
226 | | - public Paint getPaint(int strokeWidth, int color, Paint.Style style) { |
| 188 | + public Paint getPaint(int strokeWidth, Paint.Style style) { |
227 | 189 | Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); |
228 | 190 | paint.setStrokeWidth(strokeWidth); |
229 | | - paint.setColor(color); |
230 | 191 | paint.setAntiAlias(true); |
231 | 192 | paint.setStrokeCap(Paint.Cap.ROUND); |
232 | 193 | paint.setStyle(style); |
|