Browse Source

Minor refactor to Animators

Guy Carmeli 6 years ago
parent
commit
ae69d9e471

+ 14
- 16
lib/android/app/src/main/java/com/reactnativenavigation/anim/BaseAnimator.java View File

@@ -3,20 +3,24 @@ package com.reactnativenavigation.anim;
3 3
 
4 4
 import android.animation.AnimatorSet;
5 5
 import android.animation.ObjectAnimator;
6
+import android.animation.TimeInterpolator;
6 7
 import android.content.Context;
7 8
 import android.support.annotation.NonNull;
8 9
 import android.view.View;
9
-import android.view.animation.AccelerateInterpolator;
10
+import android.view.animation.AccelerateDecelerateInterpolator;
10 11
 import android.view.animation.DecelerateInterpolator;
11 12
 
12 13
 import com.reactnativenavigation.parse.AnimationsOptions;
13 14
 import com.reactnativenavigation.utils.UiUtils;
14 15
 
16
+import static android.view.View.ALPHA;
17
+import static android.view.View.TRANSLATION_Y;
18
+
15 19
 class BaseAnimator {
16 20
 
17 21
     private static final int DURATION = 300;
18
-    private static final DecelerateInterpolator DECELERATE_INTERPOLATOR = new DecelerateInterpolator();
19
-    private static final AccelerateInterpolator ACCELERATE_INTERPOLATOR = new AccelerateInterpolator();
22
+    private static final TimeInterpolator DECELERATE = new DecelerateInterpolator();
23
+    private static final TimeInterpolator ACCELERATE_DECELERATE = new AccelerateDecelerateInterpolator();
20 24
 
21 25
     protected AnimationsOptions options = new AnimationsOptions();
22 26
 
@@ -28,13 +32,11 @@ class BaseAnimator {
28 32
 
29 33
     @NonNull
30 34
     AnimatorSet getDefaultPushAnimation(View view) {
31
-        ObjectAnimator alpha = ObjectAnimator.ofFloat(view, View.ALPHA, 0, 1);
32
-        alpha.setInterpolator(DECELERATE_INTERPOLATOR);
33
-
34 35
         AnimatorSet set = new AnimatorSet();
35
-        ObjectAnimator translationY = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, this.translationY, 0);
36
-        set.setInterpolator(DECELERATE_INTERPOLATOR);
36
+        set.setInterpolator(DECELERATE);
37 37
         set.setDuration(DURATION);
38
+        ObjectAnimator translationY = ObjectAnimator.ofFloat(view, TRANSLATION_Y, this.translationY, 0);
39
+        ObjectAnimator alpha = ObjectAnimator.ofFloat(view, ALPHA, 0, 1);
38 40
         set.playTogether(translationY, alpha);
39 41
         return set;
40 42
     }
@@ -43,14 +45,10 @@ class BaseAnimator {
43 45
     @NonNull
44 46
     AnimatorSet getDefaultPopAnimation(View view) {
45 47
         AnimatorSet set = new AnimatorSet();
46
-
47
-        ObjectAnimator alpha = ObjectAnimator.ofFloat(view, View.ALPHA, 1, 0);
48
-        alpha.setInterpolator(ACCELERATE_INTERPOLATOR);
49
-
50
-        ObjectAnimator translationY = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, 0, this.translationY);
51
-        translationY.setInterpolator(ACCELERATE_INTERPOLATOR);
52
-        translationY.setDuration(DURATION);
53
-        alpha.setDuration(DURATION);
48
+        set.setInterpolator(ACCELERATE_DECELERATE);
49
+        set.setDuration(DURATION);
50
+        ObjectAnimator translationY = ObjectAnimator.ofFloat(view, TRANSLATION_Y, 0, this.translationY);
51
+        ObjectAnimator alpha = ObjectAnimator.ofFloat(view, ALPHA, 1, 0);
54 52
         set.playTogether(translationY, alpha);
55 53
         return set;
56 54
     }

+ 13
- 9
lib/android/app/src/main/java/com/reactnativenavigation/anim/TopBarAnimator.java View File

@@ -7,6 +7,7 @@ import android.animation.AnimatorSet;
7 7
 import android.animation.ObjectAnimator;
8 8
 import android.animation.TimeInterpolator;
9 9
 import android.view.View;
10
+import android.view.animation.AccelerateDecelerateInterpolator;
10 11
 import android.view.animation.DecelerateInterpolator;
11 12
 import android.view.animation.LinearInterpolator;
12 13
 
@@ -15,12 +16,15 @@ import com.reactnativenavigation.views.topbar.TopBar;
15 16
 
16 17
 import javax.annotation.Nullable;
17 18
 
19
+import static android.view.View.TRANSLATION_Y;
20
+
18 21
 public class TopBarAnimator {
19 22
 
20 23
     private static final int DEFAULT_COLLAPSE_DURATION = 100;
21
-    private static final int DURATION_TOPBAR = 300;
22
-    private final DecelerateInterpolator decelerateInterpolator = new DecelerateInterpolator();
23
-    private final LinearInterpolator linearInterpolator = new LinearInterpolator();
24
+    private static final int DURATION = 300;
25
+    private static final TimeInterpolator DECELERATE = new DecelerateInterpolator();
26
+    private static final TimeInterpolator LINEAR = new LinearInterpolator();
27
+    private static final TimeInterpolator ACCELERATE_DECELERATE = new AccelerateDecelerateInterpolator();
24 28
 
25 29
     private TopBar topBar;
26 30
     private String stackId;
@@ -40,13 +44,13 @@ public class TopBarAnimator {
40 44
         if (options.hasValue() && (!options.id.hasValue() || options.id.get().equals(stackId))) {
41 45
             showAnimator = options.getAnimation(topBar);
42 46
         } else {
43
-            showAnimator = getDefaultShowAnimator(-1 * topBar.getMeasuredHeight(), decelerateInterpolator, DURATION_TOPBAR);
47
+            showAnimator = getDefaultShowAnimator(-1 * topBar.getMeasuredHeight(), DECELERATE, DURATION);
44 48
         }
45 49
         show();
46 50
     }
47 51
 
48 52
     public void show(float startTranslation) {
49
-        showAnimator = getDefaultShowAnimator(startTranslation, linearInterpolator, DEFAULT_COLLAPSE_DURATION);
53
+        showAnimator = getDefaultShowAnimator(startTranslation, LINEAR, DEFAULT_COLLAPSE_DURATION);
50 54
         show();
51 55
     }
52 56
 
@@ -62,7 +66,7 @@ public class TopBarAnimator {
62 66
     }
63 67
 
64 68
     private AnimatorSet getDefaultShowAnimator(float startTranslation, TimeInterpolator interpolator, int duration) {
65
-        ObjectAnimator showAnimator = ObjectAnimator.ofFloat(topBar, View.TRANSLATION_Y, startTranslation, 0);
69
+        ObjectAnimator showAnimator = ObjectAnimator.ofFloat(topBar, TRANSLATION_Y, startTranslation, 0);
66 70
         showAnimator.setInterpolator(interpolator);
67 71
         showAnimator.setDuration(duration);
68 72
         AnimatorSet set = new AnimatorSet();
@@ -74,13 +78,13 @@ public class TopBarAnimator {
74 78
         if (options.hasValue() && (!options.id.hasValue() || options.id.get().equals(stackId))) {
75 79
             hideAnimator = options.getAnimation(topBar);
76 80
         } else {
77
-            hideAnimator = getDefaultHideAnimator(0, linearInterpolator, DURATION_TOPBAR);
81
+            hideAnimator = getDefaultHideAnimator(0, ACCELERATE_DECELERATE, DURATION);
78 82
         }
79 83
         hide(onAnimationEnd);
80 84
     }
81 85
 
82 86
     void hide(float startTranslation) {
83
-        hideAnimator = getDefaultHideAnimator(startTranslation, linearInterpolator, DEFAULT_COLLAPSE_DURATION);
87
+        hideAnimator = getDefaultHideAnimator(startTranslation, LINEAR, DEFAULT_COLLAPSE_DURATION);
84 88
         hide(() -> {});
85 89
     }
86 90
 
@@ -96,7 +100,7 @@ public class TopBarAnimator {
96 100
     }
97 101
 
98 102
     private Animator getDefaultHideAnimator(float startTranslation, TimeInterpolator interpolator, int duration) {
99
-        ObjectAnimator hideAnimator = ObjectAnimator.ofFloat(topBar, View.TRANSLATION_Y, startTranslation, -1 * topBar.getMeasuredHeight());
103
+        ObjectAnimator hideAnimator = ObjectAnimator.ofFloat(topBar, TRANSLATION_Y, startTranslation, -1 * topBar.getMeasuredHeight());
100 104
         hideAnimator.setInterpolator(interpolator);
101 105
         hideAnimator.setDuration(duration);
102 106
         return hideAnimator;