|
@@ -1,33 +1,57 @@
|
1
|
1
|
package com.reactnativenavigation.anim;
|
2
|
2
|
|
3
|
|
-import android.animation.Animator;
|
4
|
|
-import android.animation.AnimatorListenerAdapter;
|
5
|
|
-import android.animation.AnimatorSet;
|
6
|
|
-import android.animation.ObjectAnimator;
|
|
3
|
+import android.content.Context;
|
|
4
|
+import android.content.res.TypedArray;
|
|
5
|
+import android.support.annotation.Nullable;
|
7
|
6
|
import android.view.View;
|
8
|
|
-import android.view.animation.DecelerateInterpolator;
|
|
7
|
+import android.view.animation.Animation;
|
|
8
|
+import android.view.animation.AnimationUtils;
|
9
|
9
|
|
|
10
|
+@SuppressWarnings("ResourceType")
|
10
|
11
|
public class StackAnimator {
|
11
|
|
- public void animatePush(final View target, final Runnable onComplete) {
|
12
|
|
- target.setAlpha(0);
|
13
|
|
- target.setTranslationY(0.08f * ((View) target.getParent()).getHeight());
|
14
|
|
-
|
15
|
|
- ObjectAnimator alpha = ObjectAnimator.ofFloat(target, View.ALPHA, 1);
|
16
|
|
- alpha.setInterpolator(new DecelerateInterpolator());
|
17
|
|
- alpha.setDuration(200);
|
18
|
|
-
|
19
|
|
- ObjectAnimator translationY = ObjectAnimator.ofFloat(target, View.TRANSLATION_Y, 0);
|
20
|
|
- translationY.setInterpolator(new DecelerateInterpolator());
|
21
|
|
- translationY.setDuration(350);
|
22
|
|
-
|
23
|
|
- AnimatorSet set = new AnimatorSet();
|
24
|
|
- set.playTogether(translationY, alpha);
|
25
|
|
- set.addListener(new AnimatorListenerAdapter() {
|
26
|
|
- @Override
|
27
|
|
- public void onAnimationEnd(final Animator animation) {
|
28
|
|
- if (onComplete != null) onComplete.run();
|
29
|
|
- }
|
30
|
|
- });
|
31
|
|
- set.start();
|
|
12
|
+
|
|
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
|
+
|
|
19
|
+ public StackAnimator(Context context) {
|
|
20
|
+ this.context = context;
|
|
21
|
+ loadResIfNeeded(context);
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ private void loadResIfNeeded(Context context) {
|
|
25
|
+ if (androidOpenEnterAnimResId > 0) return;
|
|
26
|
+
|
|
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
|
+ }
|
|
35
|
+
|
|
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);
|
|
39
|
+
|
|
40
|
+ new ViewAnimationSetBuilder()
|
|
41
|
+ .withEndListener(onComplete)
|
|
42
|
+ .add(enteringView, enterAnim)
|
|
43
|
+ .add(exitingView, exitAnim)
|
|
44
|
+ .start();
|
|
45
|
+ }
|
|
46
|
+
|
|
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);
|
|
50
|
+
|
|
51
|
+ new ViewAnimationSetBuilder()
|
|
52
|
+ .withEndListener(onComplete)
|
|
53
|
+ .add(enteringView, enterAnim)
|
|
54
|
+ .add(exitingView, exitAnim)
|
|
55
|
+ .start();
|
32
|
56
|
}
|
33
|
57
|
}
|