Browse Source

Use ObjectAnimator to hide/show BottomNavigation

Guy Carmeli 8 years ago
parent
commit
a18e9d2aa0

+ 91
- 14
android/app/src/main/java/com/reactnativenavigation/views/BottomNavigation.java View File

1
 package com.reactnativenavigation.views;
1
 package com.reactnativenavigation.views;
2
 
2
 
3
+import android.animation.Animator;
4
+import android.animation.AnimatorListenerAdapter;
5
+import android.animation.ObjectAnimator;
3
 import android.content.Context;
6
 import android.content.Context;
7
+import android.support.v4.view.animation.LinearOutSlowInInterpolator;
4
 import android.util.AttributeSet;
8
 import android.util.AttributeSet;
9
+import android.util.Log;
5
 import android.view.View;
10
 import android.view.View;
6
 
11
 
7
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
12
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
10
  * Created by guyc on 10/07/16.
15
  * Created by guyc on 10/07/16.
11
  */
16
  */
12
 public class BottomNavigation extends AHBottomNavigation {
17
 public class BottomNavigation extends AHBottomNavigation {
13
-
18
+    private static final String TAG = "BottomNavigation";
14
     public static final int SCROLL_DIRECTION_UP = 0;
19
     public static final int SCROLL_DIRECTION_UP = 0;
15
     public static final int SCROLL_DIRECTION_DOWN = 1;
20
     public static final int SCROLL_DIRECTION_DOWN = 1;
16
 
21
 
22
+    private static final int STATE_HIDDEN = 0;
23
+    private static final int STATE_ANIMATE_HIDE = 1;
24
+    private static final int STATE_SHOWN = 2;
25
+    private static final int STATE_ANIMATE_SHOW = 3;
26
+    public static final int DURATION = 300;
27
+
28
+    private int mState = STATE_SHOWN;
29
+    private ObjectAnimator mHideAnimator;
30
+    private ObjectAnimator mShowAnimator;
31
+
32
+
17
     public BottomNavigation(Context context) {
33
     public BottomNavigation(Context context) {
18
         super(context);
34
         super(context);
19
     }
35
     }
26
         super(context, attrs, defStyleAttr);
42
         super(context, attrs, defStyleAttr);
27
     }
43
     }
28
 
44
 
29
-    // TODO: support animated = false -guyca
30
     public void toggleTabs(boolean hide, boolean animated) {
45
     public void toggleTabs(boolean hide, boolean animated) {
31
         if (hide) {
46
         if (hide) {
32
-            hide();
47
+            hide(animated);
33
         } else {
48
         } else {
34
-            show();
49
+            show(animated);
35
         }
50
         }
36
     }
51
     }
37
 
52
 
38
-    private void hide() {
39
-        // mBottomNavigation.hideBottomNavigation(animated);
40
-        setVisibility(View.GONE);
53
+    private void hide(boolean animated) {
54
+        if (animated) {
55
+            hideAnimated();
56
+        } else {
57
+            setVisibility(View.GONE);
58
+        }
41
     }
59
     }
42
 
60
 
43
-    private void show() {
44
-        setVisibility(View.VISIBLE);
45
-        // mBottomNavigation.restoreBottomNavigation(animated);
61
+    private void hideAnimated() {
62
+        if (mHideAnimator == null) {
63
+            mHideAnimator = createHideAnimator();
64
+        }
65
+
66
+        mHideAnimator.start();
67
+    }
68
+
69
+    private ObjectAnimator createHideAnimator() {
70
+        ObjectAnimator hideAnimator = ObjectAnimator.ofFloat(this, "translationY", getHeight());
71
+        hideAnimator.setDuration(DURATION);
72
+        hideAnimator.addListener(new AnimatorListenerAdapter() {
73
+            @Override
74
+            public void onAnimationStart(Animator animation) {
75
+                mState = STATE_ANIMATE_HIDE;
76
+            }
77
+
78
+            @Override
79
+            public void onAnimationEnd(Animator animation) {
80
+                mState = STATE_HIDDEN;
81
+            }
82
+        });
83
+        hideAnimator.setInterpolator(new LinearOutSlowInInterpolator());
84
+        return hideAnimator;
85
+
46
     }
86
     }
47
 
87
 
88
+    private void show(boolean animated) {
89
+        if (animated) {
90
+            showAnimated();
91
+        } else {
92
+            setVisibility(View.VISIBLE);
93
+        }
94
+    }
95
+
96
+    private void showAnimated() {
97
+        if (mShowAnimator == null) {
98
+            mShowAnimator = createShowAnimator();
99
+        }
100
+
101
+        mShowAnimator.start();
102
+    }
103
+
104
+    private ObjectAnimator createShowAnimator() {
105
+        ObjectAnimator showAnimator = ObjectAnimator.ofFloat(this, "translationY", 0);
106
+        showAnimator.setDuration(DURATION);
107
+        showAnimator.addListener(new AnimatorListenerAdapter() {
108
+            @Override
109
+            public void onAnimationStart(Animator animation) {
110
+                mState = STATE_ANIMATE_SHOW;
111
+            }
112
+
113
+            @Override
114
+            public void onAnimationEnd(Animator animation) {
115
+                mState = STATE_SHOWN;
116
+            }
117
+        });
118
+        showAnimator.setInterpolator(new LinearOutSlowInInterpolator());
119
+        return showAnimator;
120
+    }
121
+
122
+
48
     public void onScroll(int direction) {
123
     public void onScroll(int direction) {
49
-        if (direction == SCROLL_DIRECTION_DOWN) {
50
-            hideBottomNavigation(true);
51
-        } else if (direction == SCROLL_DIRECTION_UP) {
52
-            restoreBottomNavigation(true);
124
+        Log.d(TAG, "onScroll() called with: " + "direction = [" + direction + "]");
125
+        if (direction == SCROLL_DIRECTION_DOWN && mState == STATE_SHOWN) {
126
+            hide(true);
127
+
128
+        } else if (direction == SCROLL_DIRECTION_UP && mState == STATE_HIDDEN) {
129
+            show(true);
53
         }
130
         }
54
     }
131
     }
55
 }
132
 }