|
@@ -8,24 +8,26 @@ import android.view.View;
|
8
|
8
|
|
9
|
9
|
import com.reactnativenavigation.views.ScrollDirectionListener;
|
10
|
10
|
|
11
|
|
-public class OnScrollAnimator {
|
|
11
|
+// TODO find a better name for this class
|
|
12
|
+public class VisibilityAnimator {
|
12
|
13
|
|
13
|
14
|
public enum HideDirection {
|
14
|
15
|
Up, Down
|
15
|
16
|
}
|
16
|
17
|
|
17
|
|
- private enum State {
|
|
18
|
+ private enum VisibilityState {
|
18
|
19
|
Hidden, AnimateHide, Shown, AnimateShow
|
19
|
20
|
}
|
20
|
21
|
|
|
22
|
+ private static final int SHOW_END_VALUE = 0;
|
21
|
23
|
private static final int DURATION = 300;
|
|
24
|
+
|
22
|
25
|
private final View view;
|
23
|
26
|
private final HideDirection hideDirection;
|
24
|
27
|
private final int hiddenEndValue;
|
25
|
|
- private final int shownEndValue = 0;
|
26
|
|
- private State state = State.Shown;
|
|
28
|
+ private VisibilityState visibilityState = VisibilityState.Shown;
|
27
|
29
|
|
28
|
|
- public OnScrollAnimator(View view, HideDirection hideDirection, int height) {
|
|
30
|
+ public VisibilityAnimator(View view, HideDirection hideDirection, int height) {
|
29
|
31
|
this.view = view;
|
30
|
32
|
this.hideDirection = hideDirection;
|
31
|
33
|
this.hiddenEndValue = hideDirection == HideDirection.Up ? -height : height;
|
|
@@ -39,29 +41,47 @@ public class OnScrollAnimator {
|
39
|
41
|
}
|
40
|
42
|
}
|
41
|
43
|
|
42
|
|
- public void show() {
|
43
|
|
- ObjectAnimator animator = createAnimator(true);
|
44
|
|
- animator.start();
|
45
|
|
- }
|
46
|
|
-
|
47
|
|
- public void hide() {
|
48
|
|
- ObjectAnimator animator = createAnimator(false);
|
49
|
|
- animator.start();
|
|
44
|
+ public void setVisible(boolean visible, boolean animate) {
|
|
45
|
+ if (isShowing() && !visible) {
|
|
46
|
+ hide(animate);
|
|
47
|
+ } else if (isHiding() && visible) {
|
|
48
|
+ show(animate);
|
|
49
|
+ }
|
50
|
50
|
}
|
51
|
51
|
|
52
|
52
|
private void handleUpHidingViews(ScrollDirectionListener.Direction scrollDirection) {
|
53
|
|
- if (scrollUp(scrollDirection) && !showing()) {
|
54
|
|
- show();
|
55
|
|
- } else if (scrollDown(scrollDirection) && !hiding()) {
|
56
|
|
- hide();
|
|
53
|
+ if (scrollUp(scrollDirection) && !isShowing()) {
|
|
54
|
+ show(true);
|
|
55
|
+ } else if (scrollDown(scrollDirection) && !isHiding()) {
|
|
56
|
+ hide(true);
|
57
|
57
|
}
|
58
|
58
|
}
|
59
|
59
|
|
60
|
60
|
private void handleDownHidingViews(ScrollDirectionListener.Direction scrollDirection) {
|
61
|
|
- if (scrollDown(scrollDirection) && !hiding()) {
|
62
|
|
- hide();
|
63
|
|
- } else if (scrollUp(scrollDirection) && !showing()) {
|
64
|
|
- show();
|
|
61
|
+ if (scrollDown(scrollDirection) && !isHiding()) {
|
|
62
|
+ hide(true);
|
|
63
|
+ } else if (scrollUp(scrollDirection) && !isShowing()) {
|
|
64
|
+ show(true);
|
|
65
|
+ }
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+ private void show(boolean animate) {
|
|
69
|
+ if (animate) {
|
|
70
|
+ ObjectAnimator animator = createAnimator(true);
|
|
71
|
+ animator.start();
|
|
72
|
+ } else {
|
|
73
|
+ visibilityState = VisibilityState.Shown;
|
|
74
|
+ view.setVisibility(View.VISIBLE);
|
|
75
|
+ }
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ private void hide(boolean animate) {
|
|
79
|
+ if (animate) {
|
|
80
|
+ ObjectAnimator animator = createAnimator(false);
|
|
81
|
+ animator.start();
|
|
82
|
+ } else {
|
|
83
|
+ visibilityState = VisibilityState.Hidden;
|
|
84
|
+ view.setVisibility(View.GONE);
|
65
|
85
|
}
|
66
|
86
|
}
|
67
|
87
|
|
|
@@ -73,30 +93,29 @@ public class OnScrollAnimator {
|
73
|
93
|
return scrollDirection == ScrollDirectionListener.Direction.Down;
|
74
|
94
|
}
|
75
|
95
|
|
76
|
|
- private boolean showing() {
|
77
|
|
- return state == State.Shown || state == State.AnimateShow;
|
|
96
|
+ private boolean isShowing() {
|
|
97
|
+ return visibilityState == VisibilityState.Shown || visibilityState == VisibilityState.AnimateShow;
|
78
|
98
|
}
|
79
|
99
|
|
80
|
|
- private boolean hiding() {
|
81
|
|
- return state == State.Hidden || state == State.AnimateHide;
|
|
100
|
+ private boolean isHiding() {
|
|
101
|
+ return visibilityState == VisibilityState.Hidden || visibilityState == VisibilityState.AnimateHide;
|
82
|
102
|
}
|
83
|
103
|
|
84
|
104
|
private ObjectAnimator createAnimator(final boolean show) {
|
85
|
|
- ObjectAnimator animator = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, show ? shownEndValue : hiddenEndValue);
|
|
105
|
+ ObjectAnimator animator = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, show ? SHOW_END_VALUE : hiddenEndValue);
|
86
|
106
|
animator.setDuration(DURATION);
|
87
|
107
|
animator.setInterpolator(new LinearOutSlowInInterpolator());
|
88
|
108
|
animator.addListener(new AnimatorListenerAdapter() {
|
89
|
109
|
@Override
|
90
|
110
|
public void onAnimationStart(Animator animation) {
|
91
|
|
- state = show ? State.AnimateShow : State.AnimateHide;
|
|
111
|
+ visibilityState = show ? VisibilityState.AnimateShow : VisibilityState.AnimateHide;
|
92
|
112
|
}
|
93
|
113
|
|
94
|
114
|
@Override
|
95
|
115
|
public void onAnimationEnd(Animator animation) {
|
96
|
|
- state = show ? State.Shown : State.Hidden;
|
|
116
|
+ visibilityState = show ? VisibilityState.Shown : VisibilityState.Hidden;
|
97
|
117
|
}
|
98
|
118
|
});
|
99
|
119
|
return animator;
|
100
|
120
|
}
|
101
|
|
-
|
102
|
121
|
}
|