-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathAlpha.java
59 lines (47 loc) · 1.49 KB
/
Alpha.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
package su.levenetc.android.textsurface.animations;
import android.animation.Animator;
import android.animation.ValueAnimator;
import androidx.annotation.Nullable;
import su.levenetc.android.textsurface.utils.AnimatorEndListener;
import su.levenetc.android.textsurface.Text;
import su.levenetc.android.textsurface.interfaces.IEndListener;
/**
* Created by Eugene Levenetc.
*/
public class Alpha extends AbstractSurfaceAnimation {
private int to;
private int from;
private ValueAnimator animator;
public static Alpha hide(Text text, int duration) {
return new Alpha(text, duration, 255, 0);
}
public static Alpha show(Text text, int duration) {
return new Alpha(text, duration, 0, 255);
}
public Alpha(Text text, int duration, int from, int to) {
super(text, duration);
this.from = from;
this.to = to;
}
@Override public void start(@Nullable final IEndListener listener) {
animator = ValueAnimator.ofInt(from, to);
animator.setDuration(duration);
animator.addUpdateListener(this);
animator.addListener(new AnimatorEndListener() {
@Override public void onAnimationEnd(Animator animation) {
if (listener != null) listener.onAnimationEnd(Alpha.this);
}
});
animator.start();
}
@Override public void onAnimationUpdate(ValueAnimator animation) {
super.onAnimationUpdate(animation);
text.setAlpha((int) animation.getAnimatedValue());
}
@Override public void cancel() {
if (animator != null && animator.isRunning()) {
animator.cancel();
animator = null;
}
}
}