-
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathCircularFillableLoaders.java
More file actions
414 lines (355 loc) · 14.4 KB
/
CircularFillableLoaders.java
File metadata and controls
414 lines (355 loc) · 14.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
408
409
410
411
412
413
414
package com.mikhaellopez.circularfillableloaders;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
/**
* Created by Mikhael LOPEZ on 09/10/2015.
*/
public class CircularFillableLoaders extends ImageView {
// Default values
private static final float DEFAULT_AMPLITUDE_RATIO = 0.05f;
private static final float DEFAULT_WATER_LEVEL_RATIO = 0.5f;
private static final float DEFAULT_WAVE_LENGTH_RATIO = 1.0f;
private static final float DEFAULT_WAVE_SHIFT_RATIO = 0.0f;
public static final int DEFAULT_WAVE_COLOR = Color.BLACK;
public static final int DEFAULT_BORDER_WIDTH = 10;
// Dynamic Properties
private int canvasSize;
private float amplitudeRatio;
private int waveColor;
// Properties
private float waterLevelRatio = 1f;
private float waveShiftRatio = DEFAULT_WAVE_SHIFT_RATIO;
private float defaultWaterLevel;
// Object used to draw
private Bitmap image;
private Drawable drawable;
private Paint paint;
private Paint borderPaint;
private Paint wavePaint;
private BitmapShader waveShader;
private Matrix waveShaderMatrix;
// Animation
private AnimatorSet animatorSetWave;
//region Constructor & Init Method
public CircularFillableLoaders(final Context context) {
this(context, null);
}
public CircularFillableLoaders(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CircularFillableLoaders(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
// Init paint
paint = new Paint();
paint.setAntiAlias(true);
// Init Wave
waveShaderMatrix = new Matrix();
wavePaint = new Paint();
wavePaint.setAntiAlias(true);
// Init Animation
initAnimation();
// Load the styled attributes and set their properties
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CircularFillableLoaders, defStyleAttr, 0);
// Init Wave
waveColor = attributes.getColor(R.styleable.CircularFillableLoaders_wave_color, DEFAULT_WAVE_COLOR);
float amplitudeRatioAttr = attributes.getFloat(R.styleable.CircularFillableLoaders_wave_amplitude, DEFAULT_AMPLITUDE_RATIO);
amplitudeRatio = (amplitudeRatioAttr > DEFAULT_AMPLITUDE_RATIO) ? DEFAULT_AMPLITUDE_RATIO : amplitudeRatioAttr;
setProgress(attributes.getInteger(R.styleable.CircularFillableLoaders_progress, 0));
// Init Border
borderPaint = new Paint();
borderPaint.setAntiAlias(true);
borderPaint.setStyle(Paint.Style.STROKE);
if (attributes.getBoolean(R.styleable.CircularFillableLoaders_border, true)) {
float defaultBorderSize = DEFAULT_BORDER_WIDTH * getContext().getResources().getDisplayMetrics().density;
borderPaint.setStrokeWidth(attributes.getDimension(R.styleable.CircularFillableLoaders_border_width, defaultBorderSize));
} else {
borderPaint.setStrokeWidth(0);
}
}
//endregion
//region Draw Method
@Override
public void onDraw(Canvas canvas) {
// Load the bitmap
loadBitmap();
// Check if image isn't null
if (image == null)
return;
canvasSize = canvas.getWidth();
if (canvas.getHeight() < canvasSize) {
canvasSize = canvas.getHeight();
}
// Draw Image Circular
int circleCenter = canvasSize / 2;
canvas.drawCircle(circleCenter, circleCenter, circleCenter - borderPaint.getStrokeWidth(), paint);
// Draw Wave
// modify paint shader according to mShowWave state
if (waveShader != null) {
// first call after mShowWave, assign it to our paint
if (wavePaint.getShader() == null) {
wavePaint.setShader(waveShader);
}
// sacle shader according to waveLengthRatio and amplitudeRatio
// this decides the size(waveLengthRatio for width, amplitudeRatio for height) of waves
waveShaderMatrix.setScale(1, amplitudeRatio / DEFAULT_AMPLITUDE_RATIO, 0, defaultWaterLevel);
// translate shader according to waveShiftRatio and waterLevelRatio
// this decides the start position(waveShiftRatio for x, waterLevelRatio for y) of waves
waveShaderMatrix.postTranslate(waveShiftRatio * getWidth(),
(DEFAULT_WATER_LEVEL_RATIO - waterLevelRatio) * getHeight());
// assign matrix to invalidate the shader
waveShader.setLocalMatrix(waveShaderMatrix);
// Draw Border
borderPaint.setColor(waveColor);
float borderWidth = borderPaint.getStrokeWidth();
if (borderWidth > 0) {
canvas.drawCircle(getWidth() / 2f, getHeight() / 2f, (getWidth() - borderWidth) / 2f - 1f, borderPaint);
}
// Draw Wave
float radius = getWidth() / 2f - borderWidth;
canvas.drawCircle(getWidth() / 2f, getHeight() / 2f, radius, wavePaint);
} else {
wavePaint.setShader(null);
}
}
private void loadBitmap() {
if (this.drawable == getDrawable())
return;
this.drawable = getDrawable();
this.image = drawableToBitmap(this.drawable);
updateShader();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
canvasSize = w;
if (h < canvasSize)
canvasSize = h;
if (image != null)
updateShader();
}
private void updateShader() {
if (this.image == null)
return;
BitmapShader shader = new BitmapShader(Bitmap.createScaledBitmap(
ThumbnailUtils.extractThumbnail(image, canvasSize, canvasSize), canvasSize, canvasSize, false),
Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint.setShader(shader);
updateWaveShader();
}
private void updateWaveShader() {
double defaultAngularFrequency = 2.0f * Math.PI / DEFAULT_WAVE_LENGTH_RATIO / getWidth();
float defaultAmplitude = getHeight() * DEFAULT_AMPLITUDE_RATIO;
defaultWaterLevel = getHeight() * DEFAULT_WATER_LEVEL_RATIO;
float defaultWaveLength = getWidth();
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint wavePaint = new Paint();
wavePaint.setStrokeWidth(2);
wavePaint.setAntiAlias(true);
// Draw default waves into the bitmap
// y=Asin(ωx+φ)+h
final int endX = getWidth() + 1;
final int endY = getHeight() + 1;
float[] waveY = new float[endX];
wavePaint.setColor(adjustAlpha(waveColor, 0.3f));
for (int beginX = 0; beginX < endX; beginX++) {
double wx = beginX * defaultAngularFrequency;
float beginY = (float) (defaultWaterLevel + defaultAmplitude * Math.sin(wx));
canvas.drawLine(beginX, beginY, beginX, endY, wavePaint);
waveY[beginX] = beginY;
}
wavePaint.setColor(waveColor);
final int wave2Shift = (int) (defaultWaveLength / 4);
for (int beginX = 0; beginX < endX; beginX++) {
canvas.drawLine(beginX, waveY[(beginX + wave2Shift) % endX], beginX, endY, wavePaint);
}
// use the bitamp to create the shader
waveShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
this.wavePaint.setShader(waveShader);
}
private Bitmap drawableToBitmap(Drawable drawable) {
if (drawable == null) {
return null;
} else if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
int intrinsicWidth = drawable.getIntrinsicWidth();
int intrinsicHeight = drawable.getIntrinsicHeight();
if (!(intrinsicWidth > 0 && intrinsicHeight > 0))
return null;
try {
// Create Bitmap object out of the drawable
Bitmap bitmap = Bitmap.createBitmap(intrinsicWidth, intrinsicHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
} catch (OutOfMemoryError e) {
// Simply return null of failed bitmap creations
Log.e(getClass().toString(), "Encountered OutOfMemoryError while generating bitmap!");
return null;
}
}
//endregion
//region Mesure Method
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = measureWidth(widthMeasureSpec);
int height = measureHeight(heightMeasureSpec);
int imageSize = (width < height) ? width : height;
setMeasuredDimension(imageSize, imageSize);
}
protected int measureWidth(int measureSpec) {
int result;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
// The parent has determined an exact size for the child.
result = specSize;
} else if (specMode == MeasureSpec.AT_MOST) {
// The child can be as large as it wants up to the specified size.
result = specSize;
} else {
// The parent has not imposed any constraint on the child.
result = canvasSize;
}
return result;
}
protected int measureHeight(int measureSpecHeight) {
int result;
int specMode = MeasureSpec.getMode(measureSpecHeight);
int specSize = MeasureSpec.getSize(measureSpecHeight);
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else if (specMode == MeasureSpec.AT_MOST) {
// The child can be as large as it wants up to the specified size.
result = specSize;
} else {
// Measure the text (beware: ascent is a negative number)
result = canvasSize;
}
return (result + 2);
}
//endregion
//region Set Attr Method
public void setColor(int color) {
waveColor = color;
updateWaveShader();
invalidate();
}
public void setBorderWidth(float width) {
borderPaint.setStrokeWidth(width);
invalidate();
}
/**
* Set vertical size of wave according to <code>amplitudeRatio</code>
*
* @param amplitudeRatio Default to be 0.05. Result of amplitudeRatio + waterLevelRatio should be less than 1.
*/
public void setAmplitudeRatio(float amplitudeRatio) {
if (this.amplitudeRatio != amplitudeRatio) {
this.amplitudeRatio = amplitudeRatio;
invalidate();
}
}
public void setProgress(int progress) {
// vertical animation.
ObjectAnimator waterLevelAnim = ObjectAnimator.ofFloat(this, "waterLevelRatio", waterLevelRatio, 1f - ((float) progress / 100));
waterLevelAnim.setDuration(1000);
waterLevelAnim.setInterpolator(new DecelerateInterpolator());
AnimatorSet animatorSetProgress = new AnimatorSet();
animatorSetProgress.play(waterLevelAnim);
animatorSetProgress.start();
}
//endregion
//region Animation
private void startAnimation() {
if (animatorSetWave != null) {
animatorSetWave.start();
}
}
private void initAnimation() {
// horizontal animation.
ObjectAnimator waveShiftAnim = ObjectAnimator.ofFloat(this, "waveShiftRatio", 0f, 1f);
waveShiftAnim.setRepeatCount(ValueAnimator.INFINITE);
waveShiftAnim.setDuration(1000);
waveShiftAnim.setInterpolator(new LinearInterpolator());
animatorSetWave = new AnimatorSet();
animatorSetWave.play(waveShiftAnim);
}
/**
* Shift the wave horizontally according to <code>waveShiftRatio</code>.
*
* @param waveShiftRatio Should be 0 ~ 1. Default to be 0.
*/
private void setWaveShiftRatio(float waveShiftRatio) {
if (this.waveShiftRatio != waveShiftRatio) {
this.waveShiftRatio = waveShiftRatio;
invalidate();
}
}
/**
* Set water level according to <code>waterLevelRatio</code>.
*
* @param waterLevelRatio Should be 0 ~ 1. Default to be 0.5.
*/
private void setWaterLevelRatio(float waterLevelRatio) {
if (this.waterLevelRatio != waterLevelRatio) {
this.waterLevelRatio = waterLevelRatio;
invalidate();
}
}
private void cancel() {
if (animatorSetWave != null) {
animatorSetWave.end();
}
}
@Override
protected void onAttachedToWindow() {
startAnimation();
super.onAttachedToWindow();
}
@Override
protected void onDetachedFromWindow() {
cancel();
super.onDetachedFromWindow();
}
//endregion
/**
* Transparent the given color by the factor
* The more the factor closer to zero the more the color gets transparent
*
* @param color The color to transparent
* @param factor 1.0f to 0.0f
* @return int - A transplanted color
*/
private int adjustAlpha(int color, float factor) {
int alpha = Math.round(Color.alpha(color) * factor);
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
return Color.argb(alpha, red, green, blue);
}
}