-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircleProgress.java
410 lines (337 loc) · 11.1 KB
/
CircleProgress.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
/**
* Progress view on circle. Creates a circle with a given color and drop shadow. Setting
* animateProgress() will animate an arc on the border to the percentage given. 1 for complete, 0
* for no border.
*/
public class CircleProgress extends View {
/**
* Logging tag.
*/
static final String TAG = "CircleProgress";
/**
* Progress starts at the top of the circle.
*/
public static final float START_TOP = -90;
/**
* Progress starts at the right side of the circle.
*/
public static final float START_RIGHT = 0;
/**
* Progress starts at the bottom of the circle.
*/
public static final float START_BOTTOM = 90;
/**
* Progress starts at the left side of the circle.
*/
public static final float START_LEFT = 180;
/**
* Default border width.
*/
private static final int DEFAULT_BORDER_WIDTH = 10;
/**
* Default border color.
*/
private static final int DEFAULT_BORDER_COLOR = Color.BLUE;
/**
* Default circle color
*/
private static final int DEFAULT_CIRCLE_COLOR = Color.WHITE;
/**
* Value to use for clockwise rotation.
*/
private static final int ROTATION_CLOCKWISE = 360;
/**
* Value to use for counter clockwise rotation.
*/
private static final int ROTATION_COUNTER_CLOCKWISE = -360;
/**
* Paint to use to draw the circle.
*/
private Paint mMainPaint;
/**
* Paint to use to draw the progress meter arc.
*/
private Paint mArcPaint;
/**
* Paint to use to draw the drop shadow.
*/
private Paint mShadowPaint;
/**
* Paint for icon image.
*/
private Paint mIconPaint;
/**
* Paint to use for next icon image to use.
*/
private Paint mNextIconPaint;
/**
* Current border withd.
*/
private int mBorderWidth = 10;
/**
* The time it takes in milliseconds to animate the progress to the next set value.
*/
private int mIncrementDuration = 500;
/**
* The blur radius of the drop shadow.
*/
private int mShadowRadius = 20;
/**
* The current progress percentage. Value is from 0 - 1.
*/
private float mProgress = 0;
/**
* The percentage the progress should go to.
*/
private float mToProgress = 0;
/**
* The start angle of the progress arc.
*/
private float mStartAngle = START_TOP;
/**
* The current progress rotation.
*/
private float mRotation = ROTATION_CLOCKWISE;
/**
* Runnable instance to invoke when the progress meter reaches completion.
*/
private Runnable mOnComplete;
/**
* Icon image to display at the center of the view.
*/
private Bitmap mIconImage;
/**
* The next icon image to display at the center. Will perform a transition between current and
* this new image.
*/
private Bitmap mNextImage;
private float mFadeAlpha;
/**
* The value animator for the progress arc.
*/
private ValueAnimator mValueAnimator;
public CircleProgress(Context context) {
super(context);
mMainPaint = new Paint();
mMainPaint.setColor(DEFAULT_CIRCLE_COLOR);
mMainPaint.setAntiAlias(true);
mArcPaint = new Paint();
mArcPaint.setStyle(Paint.Style.STROKE);
mArcPaint.setStrokeWidth(DEFAULT_BORDER_WIDTH);
mArcPaint.setColor(DEFAULT_BORDER_COLOR);
mArcPaint.setAntiAlias(true);
mShadowPaint = new Paint();
setLayerType(LAYER_TYPE_SOFTWARE, mShadowPaint);
mShadowPaint.setShadowLayer(mShadowRadius, 0, 0, Color.argb(128, 0, 0, 0));
mIconPaint = new Paint();
mNextIconPaint = new Paint();
}
/**
* @see View#onDraw(Canvas)
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float cx = getWidth() / 2;
float cy = getHeight() / 2;
float delta = mShadowRadius + (mBorderWidth / 2);
canvas.drawCircle(cx, cy, getWidth() / 2 - mShadowRadius, mShadowPaint);
canvas.drawCircle(cx, cy, getWidth() / 2 - mShadowRadius, mMainPaint);
canvas.drawArc(
0 + delta, // left
0 + delta, // right
getWidth() - delta, // right
getHeight() - delta, // bottom
mStartAngle, // start angle
mRotation * mProgress, // sweep
false, // use center
mArcPaint // paint
);
if (mIconImage != null) {
mIconPaint.setAlpha((int)(255 * (1 - mFadeAlpha)));
canvas.drawBitmap(mIconImage, (getWidth() - mIconImage.getWidth()) / 2, (getHeight() - mIconImage.getHeight()) / 2, mIconPaint);
}
if (mNextImage != null) {
mNextIconPaint.setAlpha((int)(255 * mFadeAlpha));
canvas.drawBitmap(mNextImage, (getWidth() - mNextImage.getWidth()) / 2, (getHeight() - mNextImage.getHeight()) / 2, mNextIconPaint);
}
}
/**
* Animate the progress percentage to the given value.
* @param progress The value to animate to.
*/
public void animateProgress(float progress) {
// Valid values for progress are only between 0 and 1
if (progress < 0) {
progress = 0;
}
if (progress > 1) {
progress = 1;
}
final float toProgress = progress;
// No change in progress, do nothing.
if (toProgress == mToProgress) {
return;
}
Runnable runnable = new Runnable() {
@Override
public void run() {
if (mValueAnimator != null) {
mValueAnimator.cancel();
mValueAnimator = null;
}
mValueAnimator = new ValueAnimator().ofFloat(mToProgress, toProgress);
mValueAnimator.setDuration(mIncrementDuration);
mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Float value = (Float)animation.getAnimatedValue();
mProgress = value;
invalidate();
}
});
mValueAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
mToProgress = toProgress;
if (mProgress >= 1 && mOnComplete != null) {
mOnComplete.run();
}
}
@Override
public void onAnimationCancel(Animator animation) {
mToProgress = toProgress;
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
mValueAnimator.start();
}
};
Context context = getContext();
if (context instanceof Activity) {
Activity activity = (Activity)context;
activity.runOnUiThread(runnable);
}
}
/**
* Set the duration the animation to a given percentage should take. This is in milliseconds.
* @param duration The duration of the progress animation in milliseconds.
*/
public void setIncrementDuration(int duration) {
mIncrementDuration = duration;
}
/**
* Set the starting angle of the progress meter arc.
* @param startAngle The start angle in degrees from 0 to 360.
*/
public void setStartAngle(float startAngle) {
mStartAngle = startAngle;
}
/**
* Set the runnable to invoke when the progress meter is complete.
* @param runnable The runnable.
*/
public void setOnComplete(Runnable runnable) {
mOnComplete = runnable;
}
/**
* Set the width of the progress meter.
* @param width The border width.
*/
public void setBorderWidth(int width) {
mBorderWidth = width;
mArcPaint.setStrokeWidth(mBorderWidth);
}
/**
* Set the progress meter color.
* @param color The color.
*/
public void setBorderColor(int color) {
mArcPaint.setColor(color);
}
/**
* Set the circle color.
* @param color
*/
public void setCircleColor(int color) {
mMainPaint.setColor(color);
}
/**
* Reset the progress.
*/
public void reset() {
mToProgress = 0;
mProgress = 0;
invalidate();
}
/**
* Set the resource id of the icon to use in the middle of the circle view.
* @param resourceId The resource id of the drawable.
*/
public void setIcon(int resourceId) {
Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), resourceId);
mIconImage = bitmap;
}
/**
* Set whether the rotation of the progress is clockwise or counter clockwise.
* @param clockwise
*/
public void setRotationClockwise(boolean clockwise) {
mRotation = clockwise ? ROTATION_CLOCKWISE : ROTATION_COUNTER_CLOCKWISE;
}
/**
* Animate to the next icon to use. This is centered in the middle of the circle view.
* @param resourceId The resource id of the drawable to use.
*/
public void animateNextImage(int resourceId) {
Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), resourceId);
mNextImage = bitmap;
mFadeAlpha = 0;
ValueAnimator anim = new ValueAnimator().ofFloat(0, 1);
anim.setDuration(500);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Float value = (Float)animation.getAnimatedValue();
mFadeAlpha = value;
invalidate();
}
});
anim.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
mIconImage = mNextImage;
mNextImage = null;
mFadeAlpha = 0;
mIconPaint.setAlpha(255);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
anim.start();
}
}