-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompatRippleDrawable.java
645 lines (518 loc) · 18.6 KB
/
CompatRippleDrawable.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
import android.animation.AnimatorSet;
import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Outline;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import androidx.annotation.ColorInt;
import androidx.annotation.FloatRange;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.animation.PathInterpolatorCompat;
/**
* The Ripple Drawable implements an API < 21.
* API 28 based Ripple style.
*
* @author SteaI
* @version 1.0
* @since 2019-07-11
*/
public class CompatRippleDrawable extends Drawable {
private static final TimeInterpolator LINEAR_INTERPOLATOR = new LinearInterpolator();
private static final TimeInterpolator DECELERATE_INTERPOLATOR =
PathInterpolatorCompat.create(0.4f, 0f, 0.2f, 1f);
private static final int RIPPLE_ENTER_DURATION = 225;
private static final int OPACITY_ENTER_DURATION = 75;
private static final int OPACITY_EXIT_DURATION = 150;
private static final int OPACITY_HOLD_DURATION = OPACITY_ENTER_DURATION + 150;
private static final int BACKGROUND_OPACITY_DURATION = 80;
// state
private RippleState mState;
private boolean mMutated;
// measure
private float mActualCornerRadius;
// ripple
private boolean mRippleActive;
private float mStartRadius;
private float mTargetRadius;
private boolean mFocused = false;
private boolean mHovered = false;
private final RectF mBounds = new RectF();
private final RectF mCleanBounds = new RectF();
private float mX;
private float mY;
private float mStartingX;
private float mStartingY;
private float mClampedStartingX;
private float mClampedStartingY;
private final Paint mPaint;
private PorterDuffColorFilter mMaskColorFilter;
private Bitmap mMaskBuffer;
private BitmapShader mMaskShader;
private Canvas mMaskCanvas;
private Paint mMaskPaint;
private Matrix mMaskMatrix;
private Path mMaskPath;
// animation
private long mEnterStartedAtMillis;
private float mRippleOpacity;
private float mBackgroundOpacity;
private float mTweenRadius;
private float mTweenOrigin;
private AnimatorSet rippleAnimatorSet;
private ValueAnimator backgroundAnimator;
CompatRippleDrawable(RippleState state) {
mState = state;
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.FILL);
setColor(Color.argb(30, 0, 0, 0));
}
public CompatRippleDrawable() {
this(new RippleState());
}
// region Property
public int getColor() {
return mState.mColor;
}
public void setColor(@ColorInt int color) {
mState.mColor = color;
mPaint.setColor(color);
mState.mAlpha = Color.alpha(color);
// update color filter
if (mMaskColorFilter != null) {
updateMaskFilter();
}
}
public int getAlpha() {
return mState.mAlpha;
}
@Override
public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {
mState.mAlpha = alpha;
}
public void setAlpha(@FloatRange(from = 0, to = 1) float alpha) {
setAlpha((int) (255 * alpha));
}
public float getCornerRadius() {
return mState.mCornerRadius;
}
public void setCornerRadius(@FloatRange(from = 0) float cornerRadius) {
if (mState.mCornerRadius != cornerRadius) {
mState.mCornerRadius = cornerRadius;
measureCornerRadius();
updateMaskShader();
}
}
// endregion
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
@Override
public boolean isStateful() {
return true;
}
@Override
protected boolean onStateChange(int[] stateSet) {
final boolean changed = super.onStateChange(stateSet);
boolean enabled = false;
boolean pressed = false;
boolean focused = false;
boolean hovered = false;
for (int state : stateSet) {
if (state == android.R.attr.state_enabled) {
enabled = true;
} else if (state == android.R.attr.state_focused) {
focused = true;
} else if (state == android.R.attr.state_pressed) {
pressed = true;
} else if (state == android.R.attr.state_hovered) {
hovered = true;
}
}
setRippleActive(enabled && pressed);
setBackgroundActive(hovered, focused, pressed);
return changed;
}
@Override
public void setColorFilter(@Nullable ColorFilter colorFilter) {
}
private void setRippleActive(boolean active) {
if (mRippleActive != active) {
mRippleActive = active;
if (active) {
rippleEnter();
} else {
rippleExit();
}
}
}
private void setBackgroundActive(boolean hovered, boolean focused, boolean pressed) {
if (!mFocused) {
focused = focused && !pressed;
}
if (!mHovered) {
hovered = hovered && !pressed;
}
if (mHovered != hovered || mFocused != focused) {
mHovered = hovered;
mFocused = focused;
float opacity = mFocused ? 0.6f : mHovered ? 0.2f : 0f;
animateBackground(opacity);
}
}
private void cancelRipple() {
if (rippleAnimatorSet != null) {
rippleAnimatorSet.cancel();
rippleAnimatorSet = null;
}
}
private void rippleEnter() {
mTweenRadius = 0;
mTweenOrigin = 0;
mRippleOpacity = 0;
mStartingX = mX;
mStartingY = mY;
mStartRadius = Math.max(mBounds.width(), mBounds.height()) * 0.3f;
mEnterStartedAtMillis = AnimationUtils.currentAnimationTimeMillis();
clampStartingPosition();
cancelRipple();
ValueAnimator tweenAnimator = ValueAnimator.ofFloat(1);
tweenAnimator.setDuration(RIPPLE_ENTER_DURATION);
tweenAnimator.setInterpolator(DECELERATE_INTERPOLATOR);
tweenAnimator.addUpdateListener(mTweenAnimation);
ValueAnimator opacityAnimator = ValueAnimator.ofFloat(1);
opacityAnimator.setDuration(OPACITY_ENTER_DURATION);
opacityAnimator.setInterpolator(LINEAR_INTERPOLATOR);
opacityAnimator.addUpdateListener(mRippleOpacityAnimation);
rippleAnimatorSet = new AnimatorSet();
rippleAnimatorSet.play(tweenAnimator).with(opacityAnimator);
rippleAnimatorSet.start();
}
private void rippleExit() {
if (mEnterStartedAtMillis == 0) {
return;
}
long delay = computeFadeOutDelay();
long duration = AnimationUtils.currentAnimationTimeMillis() - mEnterStartedAtMillis;
float startTime = (float) (duration + delay);
float startTween = DECELERATE_INTERPOLATOR.getInterpolation(
Math.min(startTime / RIPPLE_ENTER_DURATION, 1));
float startOpacity = LINEAR_INTERPOLATOR.getInterpolation(
Math.min(startTime / OPACITY_ENTER_DURATION, 1));
ValueAnimator tweenAnimator = ValueAnimator.ofFloat(startTween, 1);
tweenAnimator.setDuration(OPACITY_EXIT_DURATION);
tweenAnimator.setInterpolator(LINEAR_INTERPOLATOR);
tweenAnimator.addUpdateListener(mTweenAnimation);
ValueAnimator opacityAnimator = ValueAnimator.ofFloat(startOpacity, 0);
opacityAnimator.setDuration(OPACITY_EXIT_DURATION);
opacityAnimator.setInterpolator(LINEAR_INTERPOLATOR);
opacityAnimator.addUpdateListener(mRippleOpacityAnimation);
rippleAnimatorSet = new AnimatorSet();
rippleAnimatorSet.setStartDelay(delay);
rippleAnimatorSet.play(tweenAnimator).with(opacityAnimator);
rippleAnimatorSet.start();
}
private void animateBackground(float opacity) {
if (backgroundAnimator != null) {
backgroundAnimator.cancel();
backgroundAnimator = null;
}
backgroundAnimator = ValueAnimator.ofFloat(mBackgroundOpacity, opacity);
backgroundAnimator.setDuration(BACKGROUND_OPACITY_DURATION);
backgroundAnimator.setInterpolator(LINEAR_INTERPOLATOR);
backgroundAnimator.addUpdateListener(mBackgroundOpacityAnimation);
backgroundAnimator.start();
}
private long computeFadeOutDelay() {
long timeSinceEnter = AnimationUtils.currentAnimationTimeMillis() - mEnterStartedAtMillis;
if (timeSinceEnter > 0 && timeSinceEnter < OPACITY_HOLD_DURATION) {
return OPACITY_HOLD_DURATION - timeSinceEnter;
}
return 0;
}
private void clampStartingPosition() {
final float cX = mBounds.centerX();
final float cY = mBounds.centerY();
final float dX = mStartingX - cX;
final float dY = mStartingY - cY;
final float r = mTargetRadius - mStartRadius;
if (dX * dX + dY * dY > r * r) {
final double angle = Math.atan2(dY, dX);
mClampedStartingX = cX + (float) (Math.cos(angle) * r);
mClampedStartingY = cY + (float) (Math.sin(angle) * r);
} else {
mClampedStartingX = mStartingX;
mClampedStartingY = mStartingY;
}
}
private void measureCornerRadius() {
float max = Math.min(mBounds.width(), mBounds.height()) / 2f;
mActualCornerRadius = Math.min(max, mState.mCornerRadius);
}
private void updateMaskShader() {
if (mState.mCornerRadius == 0 || mBounds.isEmpty()) {
if (mMaskBuffer != null) {
mMaskBuffer.recycle();
}
mMaskBuffer = null;
mMaskShader = null;
mMaskCanvas = null;
mMaskColorFilter = null;
mMaskMatrix = null;
return;
}
if (mMaskBuffer == null ||
mMaskBuffer.getWidth() != mBounds.width() ||
mMaskBuffer.getHeight() != mBounds.height()) {
if (mMaskBuffer != null) {
mMaskBuffer.recycle();
}
mMaskBuffer = Bitmap.createBitmap(
(int) mBounds.width(), (int) mBounds.height(), Bitmap.Config.ALPHA_8);
mMaskShader = new BitmapShader(mMaskBuffer, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mMaskCanvas = new Canvas(mMaskBuffer);
} else {
mMaskBuffer.eraseColor(Color.TRANSPARENT);
}
if (mMaskMatrix == null) {
mMaskMatrix = new Matrix();
} else {
mMaskMatrix.reset();
}
if (mMaskColorFilter == null) {
updateMaskFilter();
}
if (mMaskPaint == null) {
mMaskPaint = new Paint();
mMaskPaint.setAntiAlias(true);
mMaskPaint.setColor(Color.BLACK);
}
if (mMaskPath == null) {
mMaskPath = new Path();
} else {
mMaskPath.rewind();
}
mMaskPath.addRoundRect(mCleanBounds, mActualCornerRadius, mActualCornerRadius, Path.Direction.CW);
mMaskCanvas.drawPath(mMaskPath, mMaskPaint);
}
private void updateMaskFilter() {
int color = (mState.mColor & 0xFFFFFF) | (255 << 24);
mMaskColorFilter = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN);
}
private static float lerp(float start, float stop, float amount) {
return start + (stop - start) * amount;
}
private float getCurrentX() {
return lerp(mClampedStartingX - mBounds.centerX(), 0, mTweenOrigin);
}
private float getCurrentY() {
return lerp(mClampedStartingY - mBounds.centerY(), 0, mTweenOrigin);
}
private float getCurrentRadius() {
return lerp(mStartRadius, mTargetRadius, mTweenRadius);
}
@Override
public void draw(@NonNull Canvas canvas) {
float cX = mBounds.width() / 2f;
float cY = mBounds.height() / 2f;
if (mMaskShader != null) {
mPaint.setColorFilter(mMaskColorFilter);
mPaint.setShader(mMaskShader);
mMaskMatrix.setTranslate(-cX, -cY);
mMaskShader.setLocalMatrix(mMaskMatrix);
} else {
mPaint.setColorFilter(null);
mPaint.setShader(null);
}
cX += mBounds.left;
cY += mBounds.top;
int count = canvas.save();
canvas.clipRect(mBounds);
canvas.translate(cX, cY);
drawBackground(canvas);
drawRipple(canvas);
canvas.restoreToCount(count);
}
private void drawBackground(Canvas canvas) {
final int alpha = (int) (mState.mAlpha * mBackgroundOpacity + 0.5f);
if (alpha > 0) {
mPaint.setAlpha(alpha);
canvas.drawCircle(0, 0, mTargetRadius, mPaint);
mPaint.setAlpha(mState.mAlpha);
}
}
private void drawRipple(Canvas canvas) {
final int alpha = (int) (mState.mAlpha * mRippleOpacity + 0.5f);
float radius = getCurrentRadius();
if (alpha > 0 && radius > 0) {
final float x = getCurrentX();
final float y = getCurrentY();
mPaint.setAlpha(alpha);
canvas.drawCircle(x, y, radius, mPaint);
mPaint.setAlpha(mState.mAlpha);
}
}
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
mBounds.set(bounds);
mCleanBounds.left = 0;
mCleanBounds.top = 0;
mCleanBounds.right = bounds.width();
mCleanBounds.bottom = bounds.height();
measureCornerRadius();
updateMaskShader();
final float halfWidth = bounds.width() / 2.0f;
final float halfHeight = bounds.height() / 2.0f;
mTargetRadius = (float) Math.sqrt(halfWidth * halfWidth + halfHeight * halfHeight);
invalidateSelf();
}
@Override
public void setHotspot(float x, float y) {
mX = x;
mY = y;
if (mRippleActive) {
mStartingX = x;
mStartingY = y;
clampStartingPosition();
}
}
@NonNull
@Override
public Drawable mutate() {
if (!mMutated && super.mutate() == this) {
mState = new RippleState(mState);
mMutated = true;
}
return this;
}
@Override
public int getChangingConfigurations() {
return super.getChangingConfigurations() | mState.getChangingConfigurations();
}
private final FloatAnimationListener mTweenAnimation = new FloatAnimationListener() {
@Override
protected void onAnimationUpdate(float value) {
mTweenOrigin = value;
mTweenRadius = value;
invalidateSelf();
}
};
private final FloatAnimationListener mRippleOpacityAnimation = new FloatAnimationListener() {
@Override
protected void onAnimationUpdate(float value) {
mRippleOpacity = value;
invalidateSelf();
}
};
private final FloatAnimationListener mBackgroundOpacityAnimation = new FloatAnimationListener() {
@Override
protected void onAnimationUpdate(float value) {
mBackgroundOpacity = value;
invalidateSelf();
}
};
private static abstract class FloatAnimationListener implements ValueAnimator.AnimatorUpdateListener {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
onAnimationUpdate((float) animation.getAnimatedValue());
}
protected abstract void onAnimationUpdate(float value);
}
final static class RippleState extends ConstantState {
private int mChangingConfigurations;
private float mCornerRadius;
private int mColor;
private int mAlpha;
RippleState() {
}
RippleState(RippleState state) {
mChangingConfigurations = state.mChangingConfigurations;
mCornerRadius = state.mCornerRadius;
mColor = state.mColor;
mAlpha = state.mAlpha;
}
@NonNull
@Override
public Drawable newDrawable() {
return new CompatRippleDrawable(this);
}
@NonNull
@Override
public Drawable newDrawable(@Nullable Resources res) {
return new CompatRippleDrawable(this);
}
@NonNull
@Override
public Drawable newDrawable(@Nullable Resources res, @Nullable Resources.Theme theme) {
return new CompatRippleDrawable(this);
}
@Override
public int getChangingConfigurations() {
return mChangingConfigurations;
}
}
@Override
public void getOutline(@NonNull Outline outline) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
outline.setAlpha(getAlpha() / 255f);
outline.setRoundRect(getBounds(), mActualCornerRadius);
} else {
super.getOutline(outline);
}
}
public static class Builder {
private Float cornerRadius;
private Integer color;
private Integer alpha;
public Builder setCornerRadius(@FloatRange(from = 0) float value) {
cornerRadius = value;
return this;
}
public Builder setColor(@ColorInt int value) {
color = value;
return this;
}
public Builder setAlpha(@IntRange(from = 0, to = 255) int value) {
alpha = value;
return this;
}
public Builder setAlpha(@FloatRange(from = 0, to = 1) float value) {
alpha = (int) (255 * value);
return this;
}
public CompatRippleDrawable build() {
CompatRippleDrawable drawable = new CompatRippleDrawable();
if (cornerRadius != null) {
drawable.setCornerRadius(cornerRadius);
}
if (color != null) {
drawable.setColor(color);
}
if (alpha != null) {
drawable.setAlpha(alpha);
}
return drawable;
}
}
}