|
@@ -0,0 +1,104 @@
|
|
1
|
+package com.reactnativenavigation.animation;
|
|
2
|
+
|
|
3
|
+import android.animation.Animator;
|
|
4
|
+import android.animation.AnimatorListenerAdapter;
|
|
5
|
+import android.animation.ObjectAnimator;
|
|
6
|
+import android.support.annotation.NonNull;
|
|
7
|
+import android.support.v4.view.animation.LinearOutSlowInInterpolator;
|
|
8
|
+import android.view.View;
|
|
9
|
+
|
|
10
|
+import com.reactnativenavigation.views.ScrollDirectionListener;
|
|
11
|
+
|
|
12
|
+public class HideOnScrollAnimator {
|
|
13
|
+
|
|
14
|
+ public enum HideDirection {
|
|
15
|
+ Up, Down
|
|
16
|
+ }
|
|
17
|
+
|
|
18
|
+ private enum State {
|
|
19
|
+ Hidden, AnimateHide, Shown, AnimateShow
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+ private static final int DURATION = 300;
|
|
23
|
+ private final View view;
|
|
24
|
+ private final HideDirection hideDirection;
|
|
25
|
+ private final int hiddenEndValue;
|
|
26
|
+ private final int shownEndValue = 0;
|
|
27
|
+ private State state = State.Shown;
|
|
28
|
+
|
|
29
|
+ public HideOnScrollAnimator(View view, HideDirection hideDirection, int height) {
|
|
30
|
+ this.view = view;
|
|
31
|
+ this.hideDirection = hideDirection;
|
|
32
|
+ this.hiddenEndValue = hideDirection == HideDirection.Up ? -height : height;
|
|
33
|
+ }
|
|
34
|
+
|
|
35
|
+ public void onScrollChanged(ScrollDirectionListener.Direction scrollDirection) {
|
|
36
|
+ if (hideDirection == HideDirection.Down) {
|
|
37
|
+ handleDownHidingViews(scrollDirection); //TODO better name
|
|
38
|
+ } else {
|
|
39
|
+ handleUpHidingViews(scrollDirection);
|
|
40
|
+ }
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ public void show() {
|
|
44
|
+ ObjectAnimator animator = createAnimator(true);
|
|
45
|
+ animator.start();
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ public void hide() {
|
|
49
|
+ ObjectAnimator animator = createAnimator(false);
|
|
50
|
+ animator.start();
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ private void handleUpHidingViews(ScrollDirectionListener.Direction scrollDirection) {
|
|
54
|
+ if (scrollUp(scrollDirection) && !hiding()) {
|
|
55
|
+ hide();
|
|
56
|
+ } else if (scrollDown(scrollDirection) && !showing()) {
|
|
57
|
+ show();
|
|
58
|
+ }
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ private void handleDownHidingViews(ScrollDirectionListener.Direction scrollDirection) {
|
|
62
|
+ if (scrollDown(scrollDirection) && !hiding()) {
|
|
63
|
+ hide();
|
|
64
|
+ } else if (scrollUp(scrollDirection) && !showing()) {
|
|
65
|
+ show();
|
|
66
|
+ }
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ private boolean scrollUp(ScrollDirectionListener.Direction scrollDirection) {
|
|
70
|
+ return scrollDirection == ScrollDirectionListener.Direction.Up;
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ private boolean scrollDown(ScrollDirectionListener.Direction scrollDirection) {
|
|
74
|
+ return scrollDirection == ScrollDirectionListener.Direction.Down;
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ private boolean showing() {
|
|
78
|
+ return state == State.Shown || state == State.AnimateShow;
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ private boolean hiding() {
|
|
82
|
+ return state == State.Hidden || state == State.AnimateHide;
|
|
83
|
+ }
|
|
84
|
+
|
|
85
|
+ @NonNull
|
|
86
|
+ private ObjectAnimator createAnimator(final boolean show) {
|
|
87
|
+ ObjectAnimator animator = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, show ? shownEndValue : hiddenEndValue);
|
|
88
|
+ animator.setDuration(DURATION);
|
|
89
|
+ animator.setInterpolator(new LinearOutSlowInInterpolator());
|
|
90
|
+ animator.addListener(new AnimatorListenerAdapter() {
|
|
91
|
+ @Override
|
|
92
|
+ public void onAnimationStart(Animator animation) {
|
|
93
|
+ state = show ? State.AnimateShow : State.AnimateHide;
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ @Override
|
|
97
|
+ public void onAnimationEnd(Animator animation) {
|
|
98
|
+ state = show ? State.Shown : State.Hidden;
|
|
99
|
+ }
|
|
100
|
+ });
|
|
101
|
+ return animator;
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+}
|