-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathFillableLoader.java
More file actions
407 lines (349 loc) · 12.4 KB
/
FillableLoader.java
File metadata and controls
407 lines (349 loc) · 12.4 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
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
/*
* Copyright (C) 2015 Jorge Castillo Pérez
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.jorgecastillo;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.PathMeasure;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import com.github.jorgecastillo.attributes.AttributeExtractorImpl;
import com.github.jorgecastillo.clippingtransforms.ClippingTransform;
import com.github.jorgecastillo.clippingtransforms.PlainClippingTransform;
import com.github.jorgecastillo.listener.OnStateChangeListener;
import com.github.jorgecastillo.svg.ConstrainedSvgPathParser;
import com.github.jorgecastillo.svg.SvgPathParser;
import com.github.jorgecastillo.utils.MathUtil;
import java.text.ParseException;
/**
* This view is used to draw a fillable progress icon working with an SVG Path. The border
* silhouette will be the one obtained from the path.
*
* The library has been motivated by the iOS project given below.
*
* @author jorge
* @see <a href="https://github.com/poolqf/FillableLoaders.">poolqf/FillableLoaders</a>
* @since 7/08/15
*/
public class FillableLoader extends View {
private int strokeColor, fillColor, strokeWidth;
private int originalWidth, originalHeight;
private int strokeDrawingDuration, fillDuration;
private ClippingTransform clippingTransform;
private String svgPath;
private PathData pathData;
private Paint dashPaint;
private Paint fillPaint;
private int drawingState;
private long initialTime;
private int viewWidth;
private int viewHeight;
private Interpolator animInterpolator;
private OnStateChangeListener stateChangeListener;
/**
* Whether the percentage mode is enabled or not. When the percentage mode is enabled then the
* filling animation will cover part of the loader, up to the {@link #percentage} value.
*/
private boolean percentageEnabled;
/**
* The percentage that this view should load up to.
*/
private float percentage;
/**
* The percentage that the previous {@link #onDraw(Canvas)} displayed on the screen.
*/
private float previousFramePercentage;
/**
* The time in millis when the {@link #previousFramePercentage} was displayed on the screen.
*/
private long previousFramePercentageTime;
/**
* Constructor for the {@link FillableLoaderBuilder} class.
*/
FillableLoader(ViewGroup parent, ViewGroup.LayoutParams params, int strokeColor, int fillColor,
int strokeWidth, int originalWidth, int originalHeight, int strokeDrawingDuration,
int fillDuration, ClippingTransform transform, String svgPath, boolean percentageEnabled,
float fillPercentage) {
super(parent.getContext());
this.strokeColor = strokeColor;
this.fillColor = fillColor;
this.strokeWidth = strokeWidth;
this.strokeDrawingDuration = strokeDrawingDuration;
this.fillDuration = fillDuration;
this.clippingTransform = transform;
this.originalWidth = originalWidth;
this.originalHeight = originalHeight;
this.svgPath = svgPath;
this.percentageEnabled = percentageEnabled;
this.percentage = fillPercentage;
init();
parent.addView(this, params);
}
public FillableLoader(Context context) {
super(context);
init();
}
public FillableLoader(Context context, AttributeSet attrs) {
super(context, attrs);
initAttrs(attrs);
init();
}
public FillableLoader(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initAttrs(attrs);
init();
}
private void initAttrs(AttributeSet attrs) {
AttributeExtractorImpl.Builder extractorBuilder = new AttributeExtractorImpl.Builder();
AttributeExtractorImpl extractor = extractorBuilder.with(getContext()).with(attrs).build();
fillColor = extractor.getFillColor();
strokeColor = extractor.getStrokeColor();
strokeWidth = extractor.getStrokeWidth();
originalWidth = extractor.getOriginalWidth();
originalHeight = extractor.getOriginalHeight();
strokeDrawingDuration = extractor.getStrokeDrawingDuration();
fillDuration = extractor.getFillDuration();
clippingTransform = extractor.getClippingTransform();
percentage = extractor.getFillPercentage();
if (percentage != 100) {
percentageEnabled = true;
}
extractor.recycleAttributes();
}
private void init() {
drawingState = State.NOT_STARTED;
initDashPaint();
initFillPaint();
animInterpolator = new DecelerateInterpolator();
setLayerType(LAYER_TYPE_SOFTWARE, null);
}
private void initDashPaint() {
dashPaint = new Paint();
dashPaint.setStyle(Paint.Style.STROKE);
dashPaint.setAntiAlias(true);
dashPaint.setStrokeWidth(strokeWidth);
dashPaint.setColor(strokeColor);
}
private void initFillPaint() {
fillPaint = new Paint();
fillPaint.setAntiAlias(true);
fillPaint.setStyle(Paint.Style.FILL);
fillPaint.setColor(fillColor);
}
public void start() {
checkRequirements();
initialTime = System.currentTimeMillis();
changeState(State.STROKE_STARTED);
ViewCompat.postInvalidateOnAnimation(this);
}
private void checkRequirements() {
checkOriginalDimensions();
checkPath();
}
private void checkOriginalDimensions() {
if (originalWidth <= 0 || originalHeight <= 0) {
throw new IllegalArgumentException(
"You must provide the original image dimensions in order map the coordinates properly.");
}
}
private void checkPath() {
if (pathData == null) {
throw new IllegalArgumentException(
"You must provide a not empty path in order to draw the view properly.");
}
}
/**
* Resets the fillable loader. Means that the whole loader stroke + fill disappears.
*/
public void reset() {
initialTime = 0;
previousFramePercentage = 0;
changeState(State.NOT_STARTED);
ViewCompat.postInvalidateOnAnimation(this);
}
public void setToFinishedFrame() {
initialTime = 1;
changeState(State.FINISHED);
ViewCompat.postInvalidateOnAnimation(this);
}
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
viewWidth = w;
viewHeight = h;
buildPathData();
}
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!hasToDraw()) {
return;
}
long elapsedTime = System.currentTimeMillis() - initialTime;
drawStroke(canvas, elapsedTime);
if (isStrokeTotallyDrawn(elapsedTime)) {
if (drawingState < State.FILL_STARTED) {
changeState(State.FILL_STARTED);
previousFramePercentageTime = System.currentTimeMillis() - initialTime;
}
float fillPhase;
if (percentageEnabled) {
fillPhase = getFillPhaseForPercentage(elapsedTime);
} else {
fillPhase = getFillPhaseWithoutPercentage(elapsedTime);
}
clippingTransform.transform(canvas, fillPhase, this);
canvas.drawPath(pathData.path, fillPaint);
}
if (hasToKeepDrawing(elapsedTime)) {
ViewCompat.postInvalidateOnAnimation(this);
checkState();
} else {
changeState(State.FINISHED);
}
}
private void checkState() {
if (drawingState != State.FILL_FINISHED
&& percentageEnabled
&& previousFramePercentage >= percentage) {
changeState(State.FILL_FINISHED);
}
}
private void drawStroke(Canvas canvas, long elapsedTime) {
float phase = MathUtil.constrain(0, 1, elapsedTime * 1f / strokeDrawingDuration);
float distance = animInterpolator.getInterpolation(phase) * pathData.length;
dashPaint.setPathEffect(getDashPathForDistance(distance));
canvas.drawPath(pathData.path, dashPaint);
}
public boolean isStrokeTotallyDrawn(long elapsedTime) {
return elapsedTime > strokeDrawingDuration;
}
private float getFillPhaseForPercentage(long elapsedTime) {
float fillPhase = MathUtil.constrain(0, percentage / 100,
previousFramePercentage / 100 + ((float) (elapsedTime - previousFramePercentageTime)
/ fillDuration));
previousFramePercentage = fillPhase * 100;
previousFramePercentageTime = System.currentTimeMillis() - initialTime;
return fillPhase;
}
private float getFillPhaseWithoutPercentage(long elapsedTime) {
return MathUtil.constrain(0, 1, (float) (elapsedTime - strokeDrawingDuration) / fillDuration);
}
public boolean hasToDraw() {
return !(drawingState == State.NOT_STARTED || pathData == null);
}
private PathEffect getDashPathForDistance(float distance) {
return new DashPathEffect(new float[] { distance, pathData.length }, 0);
}
private boolean hasToKeepDrawing(long elapsedTime) {
if (percentageEnabled) {
return previousFramePercentage < 100;
} else {
return elapsedTime < strokeDrawingDuration + fillDuration;
}
}
public void setOnStateChangeListener(OnStateChangeListener onStateChangeListener) {
stateChangeListener = onStateChangeListener;
}
private void changeState(int state) {
if (drawingState == state) {
return;
}
drawingState = state;
if (stateChangeListener != null) {
stateChangeListener.onStateChange(state);
}
}
public void setStrokeColor(int strokeColor) {
this.strokeColor = strokeColor;
}
public void setFillColor(int fillColor) {
this.fillColor = fillColor;
}
public void setStrokeWidth(int strokeWidth) {
this.strokeWidth = strokeWidth;
}
public void setOriginalDimensions(int originalWidth, int originalHeight) {
this.originalWidth = originalWidth;
this.originalHeight = originalHeight;
}
public void setStrokeDrawingDuration(int duration) {
this.strokeDrawingDuration = duration;
}
public void setFillDuration(int duration) {
this.fillDuration = duration;
}
public void setClippingTransform(ClippingTransform transform) {
this.clippingTransform = transform == null ? new PlainClippingTransform() : transform;
}
public void setSvgPath(String svgPath) {
if (svgPath == null || svgPath.length() == 0) {
throw new IllegalArgumentException(
"You must provide a not empty path in order to draw the view properly.");
}
this.svgPath = svgPath;
buildPathData();
}
public void setPercentage(float percentage) {
if (drawingState == State.NOT_STARTED) {
this.percentageEnabled = true;
this.percentage = percentage;
} else if (drawingState == State.FINISHED) {
throw new UnsupportedOperationException("Loading has already finished.");
} else if (drawingState == State.STROKE_STARTED) {
this.percentageEnabled = true;
this.percentage = percentage;
} else if (drawingState == State.FILL_STARTED || drawingState == State.FILL_FINISHED) {
drawingState = State.FILL_STARTED;
if (percentageEnabled) {
this.percentage = percentage;
} else {
throw new UnsupportedOperationException(
"Cannot move to percentage tracking loader half way through loading");
}
ViewCompat.postInvalidateOnAnimation(this);
}
}
private void buildPathData() {
SvgPathParser parser = getPathParser();
pathData = new PathData();
try {
pathData.path = parser.parsePath(svgPath);
} catch (ParseException e) {
pathData.path = new Path();
}
PathMeasure pm = new PathMeasure(pathData.path, true);
while (true) {
pathData.length = Math.max(pathData.length, pm.getLength());
if (!pm.nextContour()) {
break;
}
}
}
private SvgPathParser getPathParser() {
ConstrainedSvgPathParser.Builder builder = new ConstrainedSvgPathParser.Builder();
return builder.originalWidth(originalWidth)
.originalHeight(originalHeight)
.viewWidth(viewWidth)
.viewHeight(viewHeight)
.build();
}
}