8
8
import android.graphics.ColorFilter;
9
9
import android.graphics.Paint;
10
10
import android.graphics.RectF;
11
+ import android.support.annotation.IntRange;
11
12
import android.view.animation.AccelerateInterpolator;
12
13
import android.view.animation.DecelerateInterpolator;
13
14
import android.view.animation.Interpolator;
14
- import android.view.animation.LinearInterpolator;
15
15
16
16
import app.dinus.com.loadingdrawable.DensityUtil;
17
17
import app.dinus.com.loadingdrawable.render.LoadingRenderer;
18
18
19
19
public class GearLoadingRenderer extends LoadingRenderer {
20
- private static final Interpolator LINEAR_INTERPOLATOR = new LinearInterpolator();
21
20
private static final Interpolator ACCELERATE_INTERPOLATOR = new AccelerateInterpolator();
22
21
private static final Interpolator DECELERATE_INTERPOLATOR = new DecelerateInterpolator();
23
22
@@ -26,7 +25,8 @@ public class GearLoadingRenderer extends LoadingRenderer {
26
25
private static final int MAX_ALPHA = 255;
27
26
private static final int DEGREE_360 = 360;
28
27
29
- private static final float MAX_SWIPE_DEGREES = 0.17f * DEGREE_360;
28
+ private static final int DEFAULT_GEAR_SWIPE_DEGREES = 60;
29
+
30
30
private static final float FULL_GROUP_ROTATION = 3.0f * DEGREE_360;
31
31
32
32
private static final float START_SCALE_DURATION_OFFSET = 0.3f;
@@ -61,6 +61,9 @@ public void onAnimationStart(Animator animation) {
61
61
62
62
private int mColor;
63
63
64
+ private int mGearCount;
65
+ private int mGearSwipeDegrees;
66
+
64
67
private float mStrokeInset;
65
68
66
69
private float mRotationCount;
@@ -82,13 +85,18 @@ private GearLoadingRenderer(Context context) {
82
85
init(context);
83
86
setupPaint();
84
87
addRenderListener(mAnimatorListener);
88
+
89
+ mDuration = 10000;
85
90
}
86
91
87
92
private void init(Context context) {
88
93
mStrokeWidth = DensityUtil.dip2px(context, DEFAULT_STROKE_WIDTH);
89
94
mCenterRadius = DensityUtil.dip2px(context, DEFAULT_CENTER_RADIUS);
90
95
91
96
mColor = DEFAULT_COLOR;
97
+
98
+ mGearCount = GEAR_COUNT;
99
+ mGearSwipeDegrees = DEFAULT_GEAR_SWIPE_DEGREES;
92
100
}
93
101
94
102
private void setupPaint() {
@@ -115,8 +123,8 @@ protected void draw(Canvas canvas) {
115
123
mPaint.setStrokeWidth(mStrokeWidth * mScale);
116
124
117
125
if (mSwipeDegrees != 0) {
118
- for (int i = 0; i < GEAR_COUNT ; i++) {
119
- canvas.drawArc(mTempBounds, mStartDegrees + DEGREE_360 / GEAR_COUNT * i, mSwipeDegrees, false, mPaint);
126
+ for (int i = 0; i < mGearCount ; i++) {
127
+ canvas.drawArc(mTempBounds, mStartDegrees + DEGREE_360 / mGearCount * i, mSwipeDegrees, false, mPaint);
120
128
}
121
129
}
122
130
@@ -136,14 +144,14 @@ protected void computeRender(float renderProgress) {
136
144
// single ring animation
137
145
if (renderProgress <= START_TRIM_DURATION_OFFSET && renderProgress > START_SCALE_DURATION_OFFSET) {
138
146
float startTrimProgress = (renderProgress - START_SCALE_DURATION_OFFSET) / (START_TRIM_DURATION_OFFSET - START_SCALE_DURATION_OFFSET);
139
- mStartDegrees = mOriginStartDegrees + MAX_SWIPE_DEGREES * LINEAR_INTERPOLATOR.getInterpolation( startTrimProgress) ;
147
+ mStartDegrees = mOriginStartDegrees + mGearSwipeDegrees * startTrimProgress;
140
148
}
141
149
142
150
// Moving the end trim starts between 50% to 80% of a single ring
143
151
// animation completes
144
152
if (renderProgress <= END_TRIM_DURATION_OFFSET && renderProgress > START_TRIM_DURATION_OFFSET) {
145
153
float endTrimProgress = (renderProgress - START_TRIM_DURATION_OFFSET) / (END_TRIM_DURATION_OFFSET - START_TRIM_DURATION_OFFSET);
146
- mEndDegrees = mOriginEndDegrees + MAX_SWIPE_DEGREES * LINEAR_INTERPOLATOR.getInterpolation( endTrimProgress) ;
154
+ mEndDegrees = mOriginEndDegrees + mGearSwipeDegrees * endTrimProgress;
147
155
}
148
156
149
157
// Scaling down the end size starts after 80% of a single ring
@@ -187,7 +195,7 @@ private void initStrokeInset(float width, float height) {
187
195
188
196
private void storeOriginals() {
189
197
mOriginEndDegrees = mEndDegrees;
190
- mOriginStartDegrees = mStartDegrees ;
198
+ mOriginStartDegrees = mEndDegrees ;
191
199
}
192
200
193
201
private void resetOriginals() {
@@ -197,7 +205,7 @@ private void resetOriginals() {
197
205
mEndDegrees = 0;
198
206
mStartDegrees = 0;
199
207
200
- mSwipeDegrees = 0 ;
208
+ mSwipeDegrees = 1 ;
201
209
}
202
210
203
211
private void apply(Builder builder) {
@@ -210,6 +218,9 @@ private void apply(Builder builder) {
210
218
211
219
this.mColor = builder.mColor != 0 ? builder.mColor : this.mColor;
212
220
221
+ this.mGearCount = builder.mGearCount > 0 ? builder.mGearCount : this.mGearCount;
222
+ this.mGearSwipeDegrees = builder.mGearSwipeDegrees > 0 ? builder.mGearSwipeDegrees : this.mGearSwipeDegrees;
223
+
213
224
setupPaint();
214
225
initStrokeInset(this.mWidth, this.mHeight);
215
226
}
@@ -226,6 +237,9 @@ public static class Builder {
226
237
227
238
private int mColor;
228
239
240
+ private int mGearCount;
241
+ private int mGearSwipeDegrees;
242
+
229
243
public Builder(Context mContext) {
230
244
this.mContext = mContext;
231
245
}
@@ -260,6 +274,16 @@ public Builder setColor(int color) {
260
274
return this;
261
275
}
262
276
277
+ public Builder setGearCount(int gearCount) {
278
+ this.mGearCount = gearCount;
279
+ return this;
280
+ }
281
+
282
+ public Builder setGearSwipeDegrees(@IntRange(from = 0, to = 360) int gearSwipeDegrees) {
283
+ this.mGearSwipeDegrees = gearSwipeDegrees;
284
+ return this;
285
+ }
286
+
263
287
public GearLoadingRenderer build() {
264
288
GearLoadingRenderer loadingRenderer = new GearLoadingRenderer(mContext);
265
289
loadingRenderer.apply(this);
0 commit comments