-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathShapeReveal.java
105 lines (83 loc) · 2.93 KB
/
ShapeReveal.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
package su.levenetc.android.textsurface.animations;
import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.Paint;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.interpolator.view.animation.FastOutSlowInInterpolator;
import su.levenetc.android.textsurface.Text;
import su.levenetc.android.textsurface.TextSurface;
import su.levenetc.android.textsurface.utils.Utils;
import su.levenetc.android.textsurface.interfaces.IEndListener;
import su.levenetc.android.textsurface.interfaces.ISurfaceAnimation;
import su.levenetc.android.textsurface.interfaces.ITextEffect;
/**
* Created by Eugene Levenetc.
*/
public class ShapeReveal implements ITextEffect, ValueAnimator.AnimatorUpdateListener {
private final Text text;
private final int duration;
private TextSurface textSurface;
private IRevealShape revealShape;
private boolean hideOnEnd;
private ValueAnimator animator;
public static ShapeReveal create(Text text, int duration, IRevealShape revealShape, boolean hideOnEnd) {
return new ShapeReveal(text, duration, revealShape, hideOnEnd);
}
private ShapeReveal(Text text, int duration, IRevealShape revealShape, boolean hideOnEnd) {
this.text = text;
this.duration = duration;
this.revealShape = revealShape;
this.hideOnEnd = hideOnEnd;
revealShape.setText(text);
}
@Override public void apply(Canvas canvas, String textValue, float x, float y, Paint paint) {
revealShape.clip(canvas, textValue, x, y, paint);
}
@Override public void setInitValues(@NonNull Text text) {
if (revealShape.isToShow()) text.setAlpha(0);
}
@NonNull @Override public Text getText() {
return text;
}
@Override public void onStart() {
text.addEffect(this);
}
@Override public void start(@Nullable final IEndListener listener) {
text.setAlpha(255);
animator = revealShape.getAnimator();
animator.setInterpolator(new FastOutSlowInInterpolator());
Utils.addEndListener(this, animator, new IEndListener() {
@Override public void onAnimationEnd(ISurfaceAnimation animation) {
text.removeEffect(ShapeReveal.this);
if (hideOnEnd) text.setAlpha(0);
if (listener != null) listener.onAnimationEnd(ShapeReveal.this);
}
});
animator.setDuration(duration);
animator.start();
}
@Override public void setTextSurface(@NonNull TextSurface textSurface) {
revealShape.setTextSurface(textSurface);
this.textSurface = textSurface;
}
@Override public long getDuration() {
return 0;
}
@Override public void cancel() {
if (animator != null && animator.isRunning()) {
animator.cancel();
animator = null;
}
}
@Override public void onAnimationUpdate(ValueAnimator animation) {
textSurface.invalidate();
}
public interface IRevealShape {
void setText(Text text);
void setTextSurface(TextSurface textSurface);
ValueAnimator getAnimator();
void clip(Canvas canvas, String textValue, float x, float y, Paint paint);
boolean isToShow();
}
}