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