-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTutorialProgressBar.java
475 lines (430 loc) · 14.3 KB
/
TutorialProgressBar.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package com.ronybrosh.tutorialprogressbar;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
/**
* Created by RonyBrosh on 1/14/2016.
*/
public class TutorialProgressBar extends View
{
private final String TAG = TutorialProgressBar.class.getSimpleName();
// Default values
private final int ANIMATION_DURATION = 1000;
private final int DEFUALT_STEPS_NUMBER = 3;
private final String DEFAULT_EMPTY_STEP_COLOR_1 = "#cdcdcd";
private final String DEFAULT_EMPTY_STEP_COLOR_2 = "#e6e6e6";
private final String DEFAULT_STEP_COLOR_1 = "#199fe4";
private final String DEFAULT_STEP_COLOR_2 = "#ed1c24";
private final boolean IS_PRINT_LOGS = false;
// Drawing values
private int[] mEmptyStepColors;
private int[] mFillStepColors;
private int[] mStepsFillPercentage;
private int[] mStepsWidth;
private Rect[] mRectArray;
// Drawing objects and parameters
private int mViewWidth;
private int mViewHeight;
private int mStepsNumber;
private int mCurrentStep;
private float mCurrentFillStepAnimationFraction;
private boolean mIsUseMask;
private ValueAnimator mValueAnimator;
private Paint mPaint;
private Bitmap mMaskBitmap;
private Bitmap mHelperBitmap;
private Canvas mHelperCanvas;
public TutorialProgressBar(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
initView();
}
public TutorialProgressBar(Context context, AttributeSet attrs)
{
super(context, attrs);
initView();
}
public TutorialProgressBar(Context context)
{
super(context);
initView();
}
/**
* Set the color array for the empty steps.
* The array length should be equal to the number of steps.
*
* @param colors
*/
public void setEmptyStepColors(int[] colors)
{
mEmptyStepColors = colors;
}
/**
* Set the color array for the filled steps.
* The array length should be equal to the number of steps.
*
* @param colors
*/
public void setFillStepColors(int[] colors)
{
mFillStepColors = colors;
}
/**
* Set array of steps fill percentage relative to the whole view.
* For example:
* Step 1 - 50%
* Step 2 - 25%
* Step 3 - 25%
* The default is equal percentage for each step.
* The array length should be equal to the number of steps.
*
* @param stepsFillPercentage
*/
public void setStepsFillPercentage(int[] stepsFillPercentage)
{
mStepsFillPercentage = stepsFillPercentage;
}
/**
* Set the number of steps.
*
* @param stepsNumber
*/
public void setStepsNumber(int stepsNumber)
{
mStepsNumber = stepsNumber;
}
/**
* Set the current step.
* Step 0 means no steps filled.
* Filling a step can be animated if desired.
*
* @param currentStep
* @param isAnimate
*/
public void setCurrentStep(int currentStep, boolean isAnimate)
{
mCurrentStep = currentStep;
if (mCurrentStep > mStepsNumber)
{
mCurrentStep = 0;
}
if (mCurrentStep > 0 && isAnimate == true)
{
mValueAnimator.cancel();
mValueAnimator.start();
}
invalidate();
}
/**
* Get the current tutorial progress step.
* Zero mean none steps are filled.
*
* @return
*/
public int getCurrentStep()
{
return mCurrentStep;
}
/**
* Set the bitmap that will be used to mask the view.
* To use the default rounded sides mask use the method
* useDefaultMask(true);
*
* @param maskBitmap
*/
public void setMask(Bitmap maskBitmap)
{
mMaskBitmap = maskBitmap;
mIsUseMask = mMaskBitmap != null ? true : false;
}
/**
* Set is used default rounded sides mask.
*
* @param isUseMask
*/
public void useDefaultMask(boolean isUseMask)
{
mIsUseMask = isUseMask;
}
/**
* View padding is not considered in calculations and shouldn't be used.
*
* @param start
* @param top
* @param end
* @param bottom
*/
@Override
public void setPaddingRelative(int start, int top, int end, int bottom)
{
if (IS_PRINT_LOGS == true)
{
Log.w(TAG, "View padding is not considered in calculations and shouldn't be used.");
}
}
/**
* View padding is not considered in calculations and shouldn't be used.
*
* @param left
* @param top
* @param right
* @param bottom
*/
@Override
public void setPadding(int left, int top, int right, int bottom)
{
if (IS_PRINT_LOGS == true)
{
Log.w(TAG, "View padding is not considered in calculations and shouldn't be used.");
}
}
/**
* View padding is not considered in calculations and shouldn't be used.
*
* @param width
* @param height
* @param oldw
* @param oldh
*/
@Override
protected void onSizeChanged(int width, int height, int oldw, int oldh)
{
super.onSizeChanged(width, height, oldw, oldh);
mViewWidth = width;
mViewHeight = height;
if (isInEditMode() == true && mViewWidth == 0 || mViewHeight == 0)
{
mViewWidth = 1;
mViewHeight = 1;
}
prepareView();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
if (mMaskBitmap != null)
{
setMeasuredDimension(mMaskBitmap.getWidth(), mMaskBitmap.getHeight());
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
@Override
protected void onDraw(Canvas canvas)
{
drawEmptySteps(mHelperCanvas);
drawFilledSteps(mHelperCanvas);
if (mIsUseMask == true)
{
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mHelperCanvas.drawBitmap(mMaskBitmap, 0, 0, mPaint);
}
canvas.drawBitmap(mHelperBitmap, 0, 0, null);
}
private void initView()
{
initPaint();
initValueAnimator();
}
private void initPaint()
{
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.FILL);
}
private void initValueAnimator()
{
mValueAnimator = ObjectAnimator.ofFloat(0, 1);
mValueAnimator.setInterpolator(new DecelerateInterpolator(0.2f));
mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
{
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator)
{
if (mCurrentStep - 1 > -1)
{
mCurrentFillStepAnimationFraction = mValueAnimator.getAnimatedFraction() * mStepsWidth[mCurrentStep - 1];
invalidate();
}
}
});
mValueAnimator.setDuration(ANIMATION_DURATION);
mValueAnimator.addListener(new Animator.AnimatorListener()
{
@Override
public void onAnimationStart(Animator arg0)
{
}
@Override
public void onAnimationRepeat(Animator arg0)
{
}
@Override
public void onAnimationEnd(Animator arg0)
{
}
@Override
public void onAnimationCancel(Animator arg0)
{
}
});
}
private void prepareView()
{
// 1. Validate steps number.
if (mStepsNumber <= 0)
{
// Set to default.
mStepsNumber = DEFUALT_STEPS_NUMBER;
}
// 2. Validate empty colors.
if (mEmptyStepColors == null || mEmptyStepColors.length != mStepsNumber)
{
// Set to default.
mEmptyStepColors = new int[mStepsNumber];
for (int stepIndex = 0; stepIndex < mStepsNumber; stepIndex++)
{
if (stepIndex % 2 != 0)
{
mEmptyStepColors[stepIndex] = Color.parseColor(DEFAULT_EMPTY_STEP_COLOR_1);
}
else
{
mEmptyStepColors[stepIndex] = Color.parseColor(DEFAULT_EMPTY_STEP_COLOR_2);
}
}
}
// 3. Validate fill colors.
if (mFillStepColors == null || mFillStepColors.length != mStepsNumber)
{
// Set to default.
mFillStepColors = new int[mStepsNumber];
for (int stepIndex = 0; stepIndex < mStepsNumber; stepIndex++)
{
if (stepIndex % 2 != 0)
{
mFillStepColors[stepIndex] = Color.parseColor(DEFAULT_STEP_COLOR_1);
}
else
{
mFillStepColors[stepIndex] = Color.parseColor(DEFAULT_STEP_COLOR_2);
}
}
}
// 4. Validate steps percentage.
if (mStepsFillPercentage == null || mStepsFillPercentage.length != mStepsNumber)
{
// Set to default.
int percentageSum = 0;
mStepsFillPercentage = new int[mStepsNumber];
for (int stepIndex = 0; stepIndex < mStepsNumber; stepIndex++)
{
percentageSum += 100 / mStepsNumber;
mStepsFillPercentage[stepIndex] = 100 / mStepsNumber;
}
// Add the extra percentage to the step in the middle.
mStepsFillPercentage[mStepsNumber / 2] += (100 - percentageSum);
}
else
{
int percentageSum = 0;
for (int nextPercentage : mStepsFillPercentage)
{
percentageSum += nextPercentage;
}
if (percentageSum != 100)
{
// Set to default.
percentageSum = 0;
mStepsFillPercentage = new int[mStepsNumber];
for (int stepIndex = 0; stepIndex < mStepsNumber; stepIndex++)
{
percentageSum += 100 / mStepsNumber;
mStepsFillPercentage[stepIndex] = 100 / mStepsNumber;
}
// Add the extra percentage to the step in the middle.
mStepsFillPercentage[mStepsNumber / 2] += (100 - percentageSum);
}
}
// 5. Init steps width and rectangles.
mRectArray = new Rect[mStepsNumber];
mStepsWidth = new int[mStepsNumber];
int widthSum = 0;
for (int stepIndex = 0; stepIndex < mStepsNumber; stepIndex++)
{
mStepsWidth[stepIndex] = (int) (mStepsFillPercentage[stepIndex] / 100f * mViewWidth);
widthSum += mStepsWidth[stepIndex];
}
mStepsWidth[mStepsNumber / 2] += (mViewWidth - widthSum);
for (int stepIndex = 0; stepIndex < mStepsNumber; stepIndex++)
{
if (stepIndex == 0)
{
mRectArray[stepIndex] = new Rect(0, 0, mStepsWidth[stepIndex], mViewHeight);
}
else
{
mRectArray[stepIndex] = new Rect(mRectArray[stepIndex - 1].right, 0, mRectArray[stepIndex - 1].right + mStepsWidth[stepIndex], mViewHeight);
}
}
// 6 Init helper bitmap and canvas.
mHelperBitmap = Bitmap.createBitmap(mViewWidth, mViewHeight, Bitmap.Config.ARGB_8888);
mHelperCanvas = new Canvas(mHelperBitmap);
// 7. Validate mask bitmap.
if (mIsUseMask == true && mMaskBitmap == null)
{
// 7.1 Create default rounded sides mask.
prepareDefaultMask();
}
}
private void prepareDefaultMask()
{
mMaskBitmap = Bitmap.createBitmap(mViewWidth, mViewHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mMaskBitmap);
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLACK);
int radius = mViewHeight / 2;
canvas.drawCircle(radius, radius, radius, paint);
canvas.drawCircle(mViewWidth - radius, radius, radius, paint);
canvas.drawRect(radius, 0, mViewWidth - radius, mViewHeight, paint);
}
private void drawEmptySteps(Canvas canvas)
{
mPaint.setXfermode(null);
for (int stepIndex = 0; stepIndex < mStepsNumber; stepIndex++)
{
mPaint.setColor(mEmptyStepColors[stepIndex]);
canvas.drawRect(mRectArray[stepIndex].left, mRectArray[stepIndex].top, mRectArray[stepIndex].right, mRectArray[stepIndex].bottom, mPaint);
}
}
private void drawFilledSteps(Canvas canvas)
{
mPaint.setXfermode(null);
for (int stepIndex = 0; stepIndex < mCurrentStep; stepIndex++)
{
mPaint.setColor(mFillStepColors[stepIndex]);
if (stepIndex == mCurrentStep - 1)
{
canvas.drawRect(mRectArray[stepIndex].left, mRectArray[stepIndex].top, mRectArray[stepIndex].left + mCurrentFillStepAnimationFraction, mRectArray[stepIndex].bottom, mPaint);
}
else
{
canvas.drawRect(mRectArray[stepIndex].left, mRectArray[stepIndex].top, mRectArray[stepIndex].right, mRectArray[stepIndex].bottom, mPaint);
}
}
}
}