-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathProgressDownloadView.java
More file actions
346 lines (276 loc) · 12.5 KB
/
ProgressDownloadView.java
File metadata and controls
346 lines (276 loc) · 12.5 KB
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
package is.arontibo.library;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.OvershootInterpolator;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;
/**
* Created by thibaultguegan on 15/02/15.
*/
public class ProgressDownloadView extends View {
private static final String LOG_TAG = ProgressDownloadView.class.getSimpleName();
public static final long ANIMATION_DURATION_BASE = 1250;
private int mWidth, mHeight, bubbleAnchorX, bubbleAnchorY, mBubbleWidth, mBubbleHeight, mPadding;
private Path mPathBlack, mPathWhite, mPathBubble;
private Paint mPaintBlack, mPaintWhite, mPaintBubble, mPaintText;
private float mDensity = getResources().getDisplayMetrics().density;
private float mProgress = 0, mTarget = 0, mSpeedAngle = 0, mBubbleAngle = 0, mFailAngle = 0, mFlipFactor;
private State mState = State.STATE_WORKING;
private enum State {
STATE_WORKING,
STATE_FAILED,
STATE_SUCCESS
}
/**
* MARK: Constructor
*/
public ProgressDownloadView(Context context, AttributeSet attrs) {
super(context, attrs);
mPadding = (int) (30 * mDensity);
mBubbleWidth = (int) (45 * mDensity);
mBubbleHeight = (int) (35 * mDensity);
setPadding(mPadding, 0, mPadding, 0);
mPaintBlack = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintBlack.setStyle(Paint.Style.STROKE);
mPaintBlack.setStrokeWidth(5 * mDensity);
mPaintBlack.setColor(getResources().getColor(R.color.red_wood));
mPaintBlack.setStrokeCap(Paint.Cap.ROUND);
//mPaintBlack.setPathEffect(new CornerPathEffect(5*mDensity));
mPaintWhite = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintWhite.setStyle(Paint.Style.STROKE);
mPaintWhite.setStrokeWidth(5 * mDensity);
mPaintWhite.setColor(Color.GREEN);
mPaintWhite.setStrokeCap(Paint.Cap.ROUND);
//mPaintWhite.setPathEffect(new CornerPathEffect(5*mDensity));
mPaintBubble = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintBubble.setColor(Color.GREEN);
mPaintBubble.setStyle(Paint.Style.FILL);
mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintText.setColor(Color.BLACK);
mPaintText.setStyle(Paint.Style.FILL);
mPaintText.setTextSize(12 * mDensity);
}
/**
* MARK: Overrides
*/
@Override
protected void onDraw(Canvas canvas) {
if (mPathWhite != null && mPathBlack != null) {
float textX = Math.max(getPaddingLeft() - (int) (mBubbleWidth / 4.0f), mProgress * mWidth / 100 - (int) (mBubbleWidth / 4.0f));
float textY = mHeight / 2 - mBubbleHeight / 2 + calculateDeltaY();
switch (mState) {
case STATE_WORKING:
// Save and restore prevent the rest of the canvas to not be rotated
canvas.save();
float speed = (getProgress() - mTarget) / 20;
mBubbleAngle += speed * 10;
if (mBubbleAngle > 20) {
mBubbleAngle = 20;
}
if (mBubbleAngle < -20) {
mBubbleAngle = -20;
}
if (Math.abs(speed) < 1) {
mSpeedAngle -= mBubbleAngle / 20;
mSpeedAngle *= .9f;
}
mBubbleAngle += mSpeedAngle;
canvas.rotate(mBubbleAngle, bubbleAnchorX, bubbleAnchorY);
canvas.drawPath(mPathBubble, mPaintBubble);
canvas.drawText(String.valueOf((int) mProgress) + " %", textX, textY, mPaintText);
canvas.restore();
break;
case STATE_FAILED:
canvas.save();
canvas.rotate(mFailAngle, bubbleAnchorX, bubbleAnchorY);
canvas.drawPath(mPathBubble, mPaintBubble);
canvas.rotate(mFailAngle, bubbleAnchorX, textY - mBubbleHeight / 7);
mPaintText.setColor(getResources().getColor(R.color.red_wine));
textX = Math.max(getPaddingLeft() - (int) (mBubbleWidth / 3.2f), mProgress * mWidth / 100 - (int) (mBubbleWidth / 3.2f));
canvas.drawText(getResources().getString(R.string.failed), textX, textY, mPaintText);
canvas.restore();
break;
case STATE_SUCCESS:
canvas.save();
mPaintText.setColor(getResources().getColor(R.color.white));
textX = Math.max(getPaddingLeft() - (int) (mBubbleWidth / 3.2f), mProgress * mWidth / 100 - (int) (mBubbleWidth / 3.2f));
Matrix flipMatrix = new Matrix();
flipMatrix.setScale(mFlipFactor, 1, bubbleAnchorX, bubbleAnchorY);
canvas.concat(flipMatrix);
canvas.drawPath(mPathBubble, mPaintBubble);
canvas.concat(flipMatrix);
canvas.drawText(getResources().getString(R.string.done), textX, textY, mPaintText);
canvas.restore();
break;
}
canvas.drawPath(mPathBlack, mPaintBlack);
canvas.drawPath(mPathWhite, mPaintWhite);
}
}
@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld) {
super.onSizeChanged(xNew, yNew, xOld, yOld);
mWidth = xNew - getPaddingRight();
mHeight = yNew;
Log.d(LOG_TAG, String.format("width and height measured are %d and %d", mWidth, mHeight));
// Call this if the enter animation is not used
//setPercentage(mProgress);
}
/**
* MARK: Update drawings
*/
private void makePathBlack() {
if (mPathBlack == null) {
mPathBlack = new Path();
}
Path p = new Path();
p.moveTo(Math.max(getPaddingLeft(), mProgress * mWidth / 100), mHeight / 2 + calculateDeltaY());
p.lineTo(mWidth, mHeight / 2);
mPathBlack.set(p);
}
private void makePathWhite() {
if (mPathWhite == null) {
mPathWhite = new Path();
}
Path p = new Path();
p.moveTo(getPaddingLeft(), mHeight / 2);
p.lineTo(Math.max(getPaddingLeft(), mProgress * mWidth / 100), mHeight / 2 + calculateDeltaY());
mPathWhite.set(p);
}
private void makePathBubble() {
if (mPathBubble == null) {
mPathBubble = new Path();
}
int width = mBubbleWidth;
int height = mBubbleHeight;
int arrowWidth = width / 3;
//Rect r = new Rect(Math.max(getPaddingLeft()-width/2-arrowWidth/4, mProgress*mWidth/100-width/2-arrowWidth/4), mHeight/2-height + calculatedeltaY(), Math.max(getPaddingLeft()+width/2-arrowWidth/4, mProgress*mWidth/100+width/2-arrowWidth/4), mHeight/2+height-height + calculatedeltaY());
Rect r = new Rect((int) (Math.max(getPaddingLeft() - width / 2, mProgress * mWidth / 100 - width / 2)), (int) (mHeight / 2 - height + calculateDeltaY()), (int) (Math.max(getPaddingLeft() + width / 2, mProgress * mWidth / 100 + width / 2)), (int) (mHeight / 2 + height - height + calculateDeltaY()));
int arrowHeight = (int) (arrowWidth / 1.5f);
int radius = 8;
Path path = new Path();
// Down arrow
path.moveTo(r.left + r.width() / 2 - arrowWidth / 2, r.top + r.height() - arrowHeight);
bubbleAnchorX = r.left + r.width() / 2;
bubbleAnchorY = r.top + r.height();
path.lineTo(bubbleAnchorX, bubbleAnchorY);
path.lineTo(r.left + r.width() / 2 + arrowWidth / 2, r.top + r.height() - arrowHeight);
// Go to bottom-right
path.lineTo(r.left + r.width() - radius, r.top + r.height() - arrowHeight);
// Bottom-right arc
path.arcTo(new RectF(r.left + r.width() - 2 * radius, r.top + r.height() - arrowHeight - 2 * radius, r.left + r.width(), r.top + r.height() - arrowHeight), 90, -90);
// Go to upper-right
path.lineTo(r.left + r.width(), r.top + arrowHeight);
// Upper-right arc
path.arcTo(new RectF(r.left + r.width() - 2 * radius, r.top, r.right, r.top + 2 * radius), 0, -90);
// Go to upper-left
path.lineTo(r.left + radius, r.top);
// Upper-left arc
path.arcTo(new RectF(r.left, r.top, r.left + 2 * radius, r.top + 2 * radius), 270, -90);
// Go to bottom-left
path.lineTo(r.left, r.top + r.height() - arrowHeight - radius);
// Bottom-left arc
path.arcTo(new RectF(r.left, r.top + r.height() - arrowHeight - 2 * radius, r.left + 2 * radius, r.top + r.height() - arrowHeight), 180, -90);
path.close();
mPathBubble.set(path);
}
/**
* MARK: Animation functions
*/
private float calculateDeltaY() {
int wireTension = 15;
if (mProgress <= 50) {
return (mProgress * mWidth / wireTension) / 50 + Math.abs((mTarget - getProgress()) / wireTension) + Math.abs(mBubbleAngle);
} else {
return ((100 - mProgress) * mWidth / wireTension) / 50 + Math.abs((mTarget - getProgress()) / wireTension) + Math.abs(mBubbleAngle);
}
}
public void setPercentage(float newProgress) {
if (newProgress < 0 || newProgress > 100) {
throw new IllegalArgumentException("setPercentage not between 0 and 100");
}
mState = State.STATE_WORKING;
mTarget = newProgress;
ObjectAnimator anim = ObjectAnimator.ofFloat(this, "progress", getProgress(), mTarget);
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration((long) (ANIMATION_DURATION_BASE + Math.abs(mTarget * 10 - getProgress() * 10) / 2));
anim.start();
}
public void setProgress(float progress) {
mProgress = progress;
makePathBlack();
makePathWhite();
makePathBubble();
invalidate();
}
public void setFailAngle(float failAngle) {
mFailAngle = failAngle;
makePathBlack();
makePathWhite();
makePathBubble();
invalidate();
}
public void setFlip(float flipValue) {
mFlipFactor = flipValue;
makePathBlack();
makePathWhite();
makePathBubble();
invalidate();
}
public float getProgress() {
return mProgress;
}
public void drawFail() {
mState = State.STATE_FAILED;
ObjectAnimator failAnim = ObjectAnimator.ofFloat(this, "failAngle", 0, 180);
failAnim.setInterpolator(new OvershootInterpolator());
//This one doesn't do much actually, we just use it to take advantage of associating two different interpolators
ObjectAnimator anim = ObjectAnimator.ofFloat(this, "progress", getProgress(), mTarget);
anim.setInterpolator(new AccelerateInterpolator());
AnimatorSet set = new AnimatorSet();
set.setDuration((long) (ANIMATION_DURATION_BASE / 1.7f));
set.playTogether(
failAnim,
anim
);
set.start();
}
public void drawSuccess() {
mTarget = 100;
final ObjectAnimator successAnim = ObjectAnimator.ofFloat(this, "flip", 1, -1);
successAnim.setInterpolator(new OvershootInterpolator());
successAnim.setDuration(ANIMATION_DURATION_BASE);
ObjectAnimator anim = ObjectAnimator.ofFloat(this, "progress", getProgress(), mTarget);
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration((long) (ANIMATION_DURATION_BASE + Math.abs(mTarget * 10 - getProgress() * 10) / 2));
anim.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
mState = State.STATE_SUCCESS;
successAnim.start();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
anim.start();
}
}