|
@@ -1,57 +1,113 @@
|
1
|
1
|
package com.reactnativenavigation.anim;
|
2
|
2
|
|
|
3
|
+import android.animation.Animator;
|
|
4
|
+import android.animation.AnimatorSet;
|
|
5
|
+import android.animation.ObjectAnimator;
|
|
6
|
+import android.app.Activity;
|
3
|
7
|
import android.content.Context;
|
4
|
|
-import android.content.res.TypedArray;
|
5
|
8
|
import android.support.annotation.Nullable;
|
|
9
|
+import android.util.DisplayMetrics;
|
6
|
10
|
import android.view.View;
|
7
|
|
-import android.view.animation.Animation;
|
8
|
|
-import android.view.animation.AnimationUtils;
|
|
11
|
+import android.view.WindowManager;
|
|
12
|
+import android.view.animation.AccelerateInterpolator;
|
|
13
|
+import android.view.animation.DecelerateInterpolator;
|
9
|
14
|
|
10
|
15
|
@SuppressWarnings("ResourceType")
|
11
|
16
|
public class StackAnimator {
|
12
|
17
|
|
13
|
|
- private static int androidOpenEnterAnimResId;
|
14
|
|
- private static int androidOpenExitAnimResId;
|
15
|
|
- private static int androidCloseEnterAnimResId;
|
16
|
|
- private static int androidCloseExitAnimResId;
|
17
|
|
- private final Context context;
|
|
18
|
+ public interface StackAnimationListener {
|
|
19
|
+ void onAnimationEnd();
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+ private static final int DURATION = 300;
|
|
23
|
+ private static final int START_DELAY = 100;
|
|
24
|
+ private static final DecelerateInterpolator DECELERATE_INTERPOLATOR = new DecelerateInterpolator();
|
|
25
|
+ private static final AccelerateInterpolator ACCELERATE_INTERPOLATOR = new AccelerateInterpolator();
|
|
26
|
+ private float translationY;
|
18
|
27
|
|
19
|
28
|
public StackAnimator(Context context) {
|
20
|
|
- this.context = context;
|
21
|
|
- loadResIfNeeded(context);
|
|
29
|
+ translationY = getWindowHeight(context);
|
22
|
30
|
}
|
23
|
31
|
|
24
|
|
- private void loadResIfNeeded(Context context) {
|
25
|
|
- if (androidOpenEnterAnimResId > 0) return;
|
|
32
|
+ public void animatePush(final View view, @Nullable final StackAnimationListener animationListener) {
|
|
33
|
+ view.setVisibility(View.INVISIBLE);
|
|
34
|
+ ObjectAnimator alpha = ObjectAnimator.ofFloat(view, View.ALPHA, 0, 1);
|
|
35
|
+ alpha.setInterpolator(DECELERATE_INTERPOLATOR);
|
26
|
36
|
|
27
|
|
- int[] attrs = {android.R.attr.activityOpenEnterAnimation, android.R.attr.activityOpenExitAnimation, android.R.attr.activityCloseEnterAnimation, android.R.attr.activityCloseExitAnimation};
|
28
|
|
- TypedArray typedArray = context.obtainStyledAttributes(android.R.style.Animation_Activity, attrs);
|
29
|
|
- androidOpenEnterAnimResId = typedArray.getResourceId(0, -1);
|
30
|
|
- androidOpenExitAnimResId = typedArray.getResourceId(1, -1);
|
31
|
|
- androidCloseEnterAnimResId = typedArray.getResourceId(2, -1);
|
32
|
|
- androidCloseExitAnimResId = typedArray.getResourceId(3, -1);
|
33
|
|
- typedArray.recycle();
|
34
|
|
- }
|
|
37
|
+ AnimatorSet set = new AnimatorSet();
|
|
38
|
+ set.addListener(new Animator.AnimatorListener() {
|
|
39
|
+ @Override
|
|
40
|
+ public void onAnimationStart(Animator animation) {
|
|
41
|
+ view.setVisibility(View.VISIBLE);
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ @Override
|
|
45
|
+ public void onAnimationEnd(Animator animation) {
|
|
46
|
+ if (animationListener != null) {
|
|
47
|
+ animationListener.onAnimationEnd();
|
|
48
|
+ }
|
|
49
|
+ }
|
35
|
50
|
|
36
|
|
- public void animatePush(final View enteringView, final View exitingView, @Nullable final Runnable onComplete) {
|
37
|
|
- Animation enterAnim = AnimationUtils.loadAnimation(context, androidOpenEnterAnimResId);
|
38
|
|
- Animation exitAnim = AnimationUtils.loadAnimation(context, androidOpenExitAnimResId);
|
|
51
|
+ @Override
|
|
52
|
+ public void onAnimationCancel(Animator animation) {
|
39
|
53
|
|
40
|
|
- new ViewAnimationSetBuilder()
|
41
|
|
- .withEndListener(onComplete)
|
42
|
|
- .add(enteringView, enterAnim)
|
43
|
|
- .add(exitingView, exitAnim)
|
44
|
|
- .start();
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ @Override
|
|
57
|
+ public void onAnimationRepeat(Animator animation) {
|
|
58
|
+
|
|
59
|
+ }
|
|
60
|
+ });
|
|
61
|
+ ObjectAnimator translationY = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, this.translationY, 0);
|
|
62
|
+ translationY.setInterpolator(DECELERATE_INTERPOLATOR);
|
|
63
|
+ translationY.setDuration(DURATION);
|
|
64
|
+ alpha.setDuration(DURATION);
|
|
65
|
+ set.playTogether(translationY, alpha);
|
|
66
|
+ set.start();
|
45
|
67
|
}
|
46
|
68
|
|
47
|
|
- public void animatePop(final View enteringView, final View exitingView, @Nullable final Runnable onComplete) {
|
48
|
|
- Animation enterAnim = AnimationUtils.loadAnimation(context, androidCloseEnterAnimResId);
|
49
|
|
- Animation exitAnim = AnimationUtils.loadAnimation(context, androidCloseExitAnimResId);
|
|
69
|
+ public void animatePop(View view, @Nullable final StackAnimationListener animationListener) {
|
|
70
|
+ ObjectAnimator alpha = ObjectAnimator.ofFloat(view, View.ALPHA, 1, 0);
|
|
71
|
+ alpha.setInterpolator(ACCELERATE_INTERPOLATOR);
|
|
72
|
+
|
|
73
|
+ AnimatorSet set = new AnimatorSet();
|
|
74
|
+ set.addListener(new Animator.AnimatorListener() {
|
|
75
|
+ @Override
|
|
76
|
+ public void onAnimationStart(Animator animation) {
|
|
77
|
+
|
|
78
|
+ }
|
50
|
79
|
|
51
|
|
- new ViewAnimationSetBuilder()
|
52
|
|
- .withEndListener(onComplete)
|
53
|
|
- .add(enteringView, enterAnim)
|
54
|
|
- .add(exitingView, exitAnim)
|
55
|
|
- .start();
|
|
80
|
+ @Override
|
|
81
|
+ public void onAnimationEnd(Animator animation) {
|
|
82
|
+ if (animationListener != null) {
|
|
83
|
+ animationListener.onAnimationEnd();
|
|
84
|
+ }
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ @Override
|
|
88
|
+ public void onAnimationCancel(Animator animation) {
|
|
89
|
+
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ @Override
|
|
93
|
+ public void onAnimationRepeat(Animator animation) {
|
|
94
|
+
|
|
95
|
+ }
|
|
96
|
+ });
|
|
97
|
+ ObjectAnimator translationY = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, 0, this.translationY);
|
|
98
|
+ translationY.setInterpolator(ACCELERATE_INTERPOLATOR);
|
|
99
|
+ translationY.setDuration(DURATION);
|
|
100
|
+ alpha.setDuration(DURATION);
|
|
101
|
+ set.playTogether(translationY, alpha);
|
|
102
|
+ set.start();
|
56
|
103
|
}
|
|
104
|
+
|
|
105
|
+ private float getWindowHeight(Context context) {
|
|
106
|
+ DisplayMetrics metrics = new DisplayMetrics();
|
|
107
|
+ WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
|
108
|
+ windowManager.getDefaultDisplay().getMetrics(metrics);
|
|
109
|
+ return metrics.heightPixels;
|
|
110
|
+ }
|
|
111
|
+
|
|
112
|
+
|
57
|
113
|
}
|