|
@@ -1,23 +1,27 @@
|
1
|
1
|
package com.reactnativenavigation.animation;
|
2
|
2
|
|
|
3
|
+import android.animation.Animator;
|
|
4
|
+import android.animation.AnimatorListenerAdapter;
|
3
|
5
|
import android.animation.ObjectAnimator;
|
4
|
6
|
import android.support.v4.view.animation.LinearOutSlowInInterpolator;
|
5
|
7
|
import android.view.View;
|
6
|
8
|
|
7
|
9
|
public class VisibilityAnimator {
|
8
|
10
|
|
9
|
|
- private final LinearOutSlowInInterpolator interpolator = new LinearOutSlowInInterpolator();
|
10
|
|
- private ObjectAnimator animator;
|
11
|
|
-
|
12
|
11
|
public enum HideDirection {
|
13
|
12
|
Up, Down
|
14
|
13
|
}
|
15
|
14
|
|
|
15
|
+ private enum VisibilityState {
|
|
16
|
+ Hidden, AnimateHide, Shown, AnimateShow
|
|
17
|
+ }
|
|
18
|
+
|
16
|
19
|
private static final int SHOW_END_VALUE = 0;
|
17
|
20
|
private static final int DURATION = 300;
|
18
|
21
|
|
19
|
22
|
private final View view;
|
20
|
23
|
private final int hiddenEndValue;
|
|
24
|
+ private VisibilityState visibilityState = VisibilityState.Shown;
|
21
|
25
|
|
22
|
26
|
public VisibilityAnimator(View view, HideDirection hideDirection, int height) {
|
23
|
27
|
this.view = view;
|
|
@@ -25,45 +29,56 @@ public class VisibilityAnimator {
|
25
|
29
|
}
|
26
|
30
|
|
27
|
31
|
public void setVisible(boolean visible, boolean animate) {
|
28
|
|
- cancelAnimator();
|
29
|
|
- if (visible) {
|
|
32
|
+ if (visible && isHiding()) {
|
30
|
33
|
show(animate);
|
31
|
|
- } else {
|
|
34
|
+ } else if (!visible && isShowing()) {
|
32
|
35
|
hide(animate);
|
33
|
36
|
}
|
34
|
37
|
}
|
35
|
38
|
|
36
|
|
- private void cancelAnimator() {
|
37
|
|
- if (animator != null && animator.isRunning()) {
|
38
|
|
- view.clearAnimation();
|
39
|
|
- animator.cancel();
|
40
|
|
- }
|
41
|
|
- }
|
42
|
|
-
|
43
|
39
|
private void show(boolean animate) {
|
44
|
40
|
if (animate) {
|
45
|
|
- animator = createAnimator(true);
|
|
41
|
+ ObjectAnimator animator = createAnimator(true);
|
46
|
42
|
animator.start();
|
47
|
43
|
} else {
|
48
|
|
- view.setTranslationY(SHOW_END_VALUE);
|
49
|
|
- view.setY(SHOW_END_VALUE);
|
|
44
|
+ visibilityState = VisibilityState.Shown;
|
|
45
|
+ view.setVisibility(View.VISIBLE);
|
50
|
46
|
}
|
51
|
47
|
}
|
52
|
48
|
|
53
|
49
|
private void hide(boolean animate) {
|
54
|
50
|
if (animate) {
|
55
|
|
- animator = createAnimator(false);
|
|
51
|
+ ObjectAnimator animator = createAnimator(false);
|
56
|
52
|
animator.start();
|
57
|
53
|
} else {
|
58
|
|
- view.setTranslationY(hiddenEndValue);
|
59
|
|
- view.setY(hiddenEndValue);
|
|
54
|
+ visibilityState = VisibilityState.Hidden;
|
|
55
|
+ view.setVisibility(View.GONE);
|
60
|
56
|
}
|
61
|
57
|
}
|
62
|
58
|
|
|
59
|
+ private boolean isShowing() {
|
|
60
|
+ return visibilityState == VisibilityState.Shown || visibilityState == VisibilityState.AnimateShow;
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ private boolean isHiding() {
|
|
64
|
+ return visibilityState == VisibilityState.Hidden || visibilityState == VisibilityState.AnimateHide;
|
|
65
|
+ }
|
|
66
|
+
|
63
|
67
|
private ObjectAnimator createAnimator(final boolean show) {
|
64
|
|
- final ObjectAnimator animator = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, show ? SHOW_END_VALUE : hiddenEndValue);
|
|
68
|
+ ObjectAnimator animator = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, show ? SHOW_END_VALUE : hiddenEndValue);
|
65
|
69
|
animator.setDuration(DURATION);
|
66
|
|
- animator.setInterpolator(interpolator);
|
|
70
|
+ animator.setInterpolator(new LinearOutSlowInInterpolator());
|
|
71
|
+ animator.addListener(new AnimatorListenerAdapter() {
|
|
72
|
+ @Override
|
|
73
|
+ public void onAnimationStart(Animator animation) {
|
|
74
|
+ visibilityState = show ? VisibilityState.AnimateShow : VisibilityState.AnimateHide;
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ @Override
|
|
78
|
+ public void onAnimationEnd(Animator animation) {
|
|
79
|
+ visibilityState = show ? VisibilityState.Shown : VisibilityState.Hidden;
|
|
80
|
+ }
|
|
81
|
+ });
|
67
|
82
|
return animator;
|
68
|
83
|
}
|
69
|
84
|
}
|