Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 872c6bf

Browse files
author
dinuscxj
committed
optimize circle rotate series code
1 parent befbbd9 commit 872c6bf

File tree

4 files changed

+37
-31
lines changed

4 files changed

+37
-31
lines changed

library/src/main/java/app/dinus/com/loadingdrawable/render/circle/rotate/GearLoadingRenderer.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@
88
import android.graphics.ColorFilter;
99
import android.graphics.Paint;
1010
import android.graphics.RectF;
11+
import android.support.annotation.IntRange;
1112
import android.view.animation.AccelerateInterpolator;
1213
import android.view.animation.DecelerateInterpolator;
1314
import android.view.animation.Interpolator;
14-
import android.view.animation.LinearInterpolator;
1515

1616
import app.dinus.com.loadingdrawable.DensityUtil;
1717
import app.dinus.com.loadingdrawable.render.LoadingRenderer;
1818

1919
public class GearLoadingRenderer extends LoadingRenderer {
20-
private static final Interpolator LINEAR_INTERPOLATOR = new LinearInterpolator();
2120
private static final Interpolator ACCELERATE_INTERPOLATOR = new AccelerateInterpolator();
2221
private static final Interpolator DECELERATE_INTERPOLATOR = new DecelerateInterpolator();
2322

@@ -26,7 +25,8 @@ public class GearLoadingRenderer extends LoadingRenderer {
2625
private static final int MAX_ALPHA = 255;
2726
private static final int DEGREE_360 = 360;
2827

29-
private static final float MAX_SWIPE_DEGREES = 0.17f * DEGREE_360;
28+
private static final int DEFAULT_GEAR_SWIPE_DEGREES = 60;
29+
3030
private static final float FULL_GROUP_ROTATION = 3.0f * DEGREE_360;
3131

3232
private static final float START_SCALE_DURATION_OFFSET = 0.3f;
@@ -61,6 +61,9 @@ public void onAnimationStart(Animator animation) {
6161

6262
private int mColor;
6363

64+
private int mGearCount;
65+
private int mGearSwipeDegrees;
66+
6467
private float mStrokeInset;
6568

6669
private float mRotationCount;
@@ -82,13 +85,18 @@ private GearLoadingRenderer(Context context) {
8285
init(context);
8386
setupPaint();
8487
addRenderListener(mAnimatorListener);
88+
89+
mDuration = 10000;
8590
}
8691

8792
private void init(Context context) {
8893
mStrokeWidth = DensityUtil.dip2px(context, DEFAULT_STROKE_WIDTH);
8994
mCenterRadius = DensityUtil.dip2px(context, DEFAULT_CENTER_RADIUS);
9095

9196
mColor = DEFAULT_COLOR;
97+
98+
mGearCount = GEAR_COUNT;
99+
mGearSwipeDegrees = DEFAULT_GEAR_SWIPE_DEGREES;
92100
}
93101

94102
private void setupPaint() {
@@ -115,8 +123,8 @@ protected void draw(Canvas canvas) {
115123
mPaint.setStrokeWidth(mStrokeWidth * mScale);
116124

117125
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);
120128
}
121129
}
122130

@@ -136,14 +144,14 @@ protected void computeRender(float renderProgress) {
136144
// single ring animation
137145
if (renderProgress <= START_TRIM_DURATION_OFFSET && renderProgress > START_SCALE_DURATION_OFFSET) {
138146
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;
140148
}
141149

142150
// Moving the end trim starts between 50% to 80% of a single ring
143151
// animation completes
144152
if (renderProgress <= END_TRIM_DURATION_OFFSET && renderProgress > START_TRIM_DURATION_OFFSET) {
145153
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;
147155
}
148156

149157
// Scaling down the end size starts after 80% of a single ring
@@ -187,7 +195,7 @@ private void initStrokeInset(float width, float height) {
187195

188196
private void storeOriginals() {
189197
mOriginEndDegrees = mEndDegrees;
190-
mOriginStartDegrees = mStartDegrees;
198+
mOriginStartDegrees = mEndDegrees;
191199
}
192200

193201
private void resetOriginals() {
@@ -197,7 +205,7 @@ private void resetOriginals() {
197205
mEndDegrees = 0;
198206
mStartDegrees = 0;
199207

200-
mSwipeDegrees = 0;
208+
mSwipeDegrees = 1;
201209
}
202210

203211
private void apply(Builder builder) {
@@ -210,6 +218,9 @@ private void apply(Builder builder) {
210218

211219
this.mColor = builder.mColor != 0 ? builder.mColor : this.mColor;
212220

221+
this.mGearCount = builder.mGearCount > 0 ? builder.mGearCount : this.mGearCount;
222+
this.mGearSwipeDegrees = builder.mGearSwipeDegrees > 0 ? builder.mGearSwipeDegrees : this.mGearSwipeDegrees;
223+
213224
setupPaint();
214225
initStrokeInset(this.mWidth, this.mHeight);
215226
}
@@ -226,6 +237,9 @@ public static class Builder {
226237

227238
private int mColor;
228239

240+
private int mGearCount;
241+
private int mGearSwipeDegrees;
242+
229243
public Builder(Context mContext) {
230244
this.mContext = mContext;
231245
}
@@ -260,6 +274,16 @@ public Builder setColor(int color) {
260274
return this;
261275
}
262276

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+
263287
public GearLoadingRenderer build() {
264288
GearLoadingRenderer loadingRenderer = new GearLoadingRenderer(mContext);
265289
loadingRenderer.apply(this);

library/src/main/java/app/dinus/com/loadingdrawable/render/circle/rotate/LevelLoadingRenderer.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,8 @@ public void onAnimationStart(Animator animation) {
7575

7676
private float mEndDegrees;
7777
private float mStartDegrees;
78-
private float mRotationIncrement;
7978
private float mOriginEndDegrees;
8079
private float mOriginStartDegrees;
81-
private float mOriginRotationIncrement;
8280

8381
private float mStrokeWidth;
8482
private float mCenterRadius;
@@ -170,7 +168,6 @@ protected void computeRender(float renderProgress) {
170168
}
171169

172170
mGroupRotation = ((FULL_GROUP_ROTATION / NUM_POINTS) * renderProgress) + (FULL_GROUP_ROTATION * (mRotationCount / NUM_POINTS));
173-
mRotationIncrement = mOriginRotationIncrement + (MAX_ROTATION_INCREMENT * renderProgress);
174171
}
175172

176173
@Override
@@ -197,18 +194,15 @@ private void initStrokeInset(float width, float height) {
197194

198195
private void storeOriginals() {
199196
mOriginEndDegrees = mEndDegrees;
200-
mOriginStartDegrees = mStartDegrees;
201-
mOriginRotationIncrement = mRotationIncrement;
197+
mOriginStartDegrees = mEndDegrees;
202198
}
203199

204200
private void resetOriginals() {
205201
mOriginEndDegrees = 0;
206202
mOriginStartDegrees = 0;
207-
mOriginRotationIncrement = 0;
208203

209204
mEndDegrees = 0;
210205
mStartDegrees = 0;
211-
mRotationIncrement = 0;
212206

213207
mLevelSwipeDegrees[0] = 0;
214208
mLevelSwipeDegrees[1] = 0;

library/src/main/java/app/dinus/com/loadingdrawable/render/circle/rotate/MaterialLoadingRenderer.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@ public void onAnimationStart(Animator animation) {
6868
private float mEndDegrees;
6969
private float mStartDegrees;
7070
private float mSwipeDegrees;
71-
private float mRotationIncrement;
7271
private float mOriginEndDegrees;
7372
private float mOriginStartDegrees;
74-
private float mOriginRotationIncrement;
7573

7674
private float mStrokeWidth;
7775
private float mCenterRadius;
@@ -144,7 +142,6 @@ protected void computeRender(float renderProgress) {
144142

145143
mGroupRotation = ((FULL_GROUP_ROTATION / NUM_POINTS) * renderProgress)
146144
+ (FULL_GROUP_ROTATION * (mRotationCount / NUM_POINTS));
147-
mRotationIncrement = mOriginRotationIncrement + (MAX_ROTATION_INCREMENT * renderProgress);
148145
}
149146

150147
@Override
@@ -188,18 +185,15 @@ private void initStrokeInset(float width, float height) {
188185

189186
private void storeOriginals() {
190187
mOriginEndDegrees = mEndDegrees;
191-
mOriginStartDegrees = mStartDegrees;
192-
mOriginRotationIncrement = mRotationIncrement;
188+
mOriginStartDegrees = mEndDegrees;
193189
}
194190

195191
private void resetOriginals() {
196192
mOriginEndDegrees = 0;
197193
mOriginStartDegrees = 0;
198-
mOriginRotationIncrement = 0;
199194

200195
mEndDegrees = 0;
201196
mStartDegrees = 0;
202-
mRotationIncrement = 0;
203197
}
204198

205199
private int getStartingColor() {

library/src/main/java/app/dinus/com/loadingdrawable/render/circle/rotate/WhorlLoadingRenderer.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,9 @@ public void onAnimationStart(Animator animation) {
6868
private float mEndDegrees;
6969
private float mStartDegrees;
7070
private float mSwipeDegrees;
71-
private float mRotationIncrement;
7271
private float mOriginEndDegrees;
7372
private float mOriginStartDegrees;
74-
private float mOriginRotationIncrement;
75-
73+
7674
private float mStrokeWidth;
7775
private float mCenterRadius;
7876

@@ -156,7 +154,6 @@ protected void computeRender(float renderProgress) {
156154
}
157155

158156
mGroupRotation = ((FULL_GROUP_ROTATION / NUM_POINTS) * renderProgress) + (FULL_GROUP_ROTATION * (mRotationCount / NUM_POINTS));
159-
mRotationIncrement = mOriginRotationIncrement + (MAX_ROTATION_INCREMENT * renderProgress);
160157
}
161158

162159
@Override
@@ -185,18 +182,15 @@ private void initStrokeInset(float width, float height) {
185182

186183
private void storeOriginals() {
187184
mOriginEndDegrees = mEndDegrees;
188-
mOriginStartDegrees = mStartDegrees;
189-
mOriginRotationIncrement = mRotationIncrement;
185+
mOriginStartDegrees = mEndDegrees;
190186
}
191187

192188
private void resetOriginals() {
193189
mOriginEndDegrees = 0;
194190
mOriginStartDegrees = 0;
195-
mOriginRotationIncrement = 0;
196191

197192
mEndDegrees = 0;
198193
mStartDegrees = 0;
199-
mRotationIncrement = 0;
200194

201195
mSwipeDegrees = 0;
202196
}

0 commit comments

Comments
 (0)