react-native-navigation的迁移库

NavigationAnimator.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.reactnativenavigation.anim;
  2. import android.animation.Animator;
  3. import android.animation.AnimatorListenerAdapter;
  4. import android.animation.AnimatorSet;
  5. import android.content.Context;
  6. import android.view.View;
  7. import com.reactnativenavigation.parse.AnimationOptions;
  8. import com.reactnativenavigation.parse.FadeAnimation;
  9. import com.reactnativenavigation.parse.NestedAnimationsOptions;
  10. import com.reactnativenavigation.parse.Options;
  11. import com.reactnativenavigation.viewcontrollers.ViewController;
  12. import com.reactnativenavigation.views.element.ElementTransitionManager;
  13. import java.util.ArrayList;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. import androidx.annotation.RestrictTo;
  17. import static com.reactnativenavigation.utils.CollectionUtils.*;
  18. @SuppressWarnings("ResourceType")
  19. public class NavigationAnimator extends BaseAnimator {
  20. private final ElementTransitionManager transitionManager;
  21. private Map<View, Animator> runningPushAnimations = new HashMap<>();
  22. public NavigationAnimator(Context context, ElementTransitionManager transitionManager) {
  23. super(context);
  24. this.transitionManager = transitionManager;
  25. }
  26. public void push(ViewController appearing, ViewController disappearing, Options options, Runnable onAnimationEnd) {
  27. appearing.getView().setAlpha(0);
  28. transitionManager.createTransitions(
  29. options.animations.push,
  30. disappearing,
  31. appearing,
  32. transitionSet -> {
  33. AnimatorSet set = new AnimatorSet();
  34. runningPushAnimations.put(appearing.getView(), set);
  35. set.addListener(new AnimatorListenerAdapter() {
  36. private boolean isCancelled;
  37. @Override
  38. public void onAnimationCancel(Animator animation) {
  39. isCancelled = true;
  40. runningPushAnimations.remove(appearing.getView());
  41. onAnimationEnd.run();
  42. }
  43. @Override
  44. public void onAnimationEnd(Animator animation) {
  45. if (!isCancelled) {
  46. runningPushAnimations.remove(appearing.getView());
  47. onAnimationEnd.run();
  48. }
  49. }
  50. });
  51. if (transitionSet.isEmpty()) {
  52. set.playTogether(options.animations.push.content.getAnimation(appearing.getView(), getDefaultPushAnimation(appearing.getView())));
  53. } else {
  54. AnimationOptions fade = options.animations.push.content.isFadeAnimation() ? options.animations.push.content : new FadeAnimation().content;
  55. AnimatorSet transitions = transitionManager.createAnimators(fade, transitionSet);
  56. ArrayList<Animator.AnimatorListener> listeners = transitions.getListeners();
  57. set.playTogether(fade.getAnimation(appearing.getView()), transitions);
  58. forEach(listeners, set::addListener);
  59. transitions.removeAllListeners();
  60. }
  61. set.start();
  62. }
  63. );
  64. }
  65. public void pop(View view, NestedAnimationsOptions pop, Runnable onAnimationEnd) {
  66. if (runningPushAnimations.containsKey(view)) {
  67. runningPushAnimations.get(view).cancel();
  68. onAnimationEnd.run();
  69. return;
  70. }
  71. AnimatorSet set = pop.content.getAnimation(view, getDefaultPopAnimation(view));
  72. set.addListener(new AnimatorListenerAdapter() {
  73. private boolean cancelled;
  74. @Override
  75. public void onAnimationCancel(Animator animation) {
  76. this.cancelled = true;
  77. }
  78. @Override
  79. public void onAnimationEnd(Animator animation) {
  80. if (!cancelled) onAnimationEnd.run();
  81. }
  82. });
  83. set.start();
  84. }
  85. public void setRoot(View root, AnimationOptions setRoot, Runnable onAnimationEnd) {
  86. root.setVisibility(View.INVISIBLE);
  87. AnimatorSet set = setRoot.getAnimation(root);
  88. set.addListener(new AnimatorListenerAdapter() {
  89. @Override
  90. public void onAnimationStart(Animator animation) {
  91. root.setVisibility(View.VISIBLE);
  92. }
  93. @Override
  94. public void onAnimationEnd(Animator animation) {
  95. onAnimationEnd.run();
  96. }
  97. });
  98. set.start();
  99. }
  100. public void cancelPushAnimations() {
  101. for (View view : runningPushAnimations.keySet()) {
  102. runningPushAnimations.get(view).cancel();
  103. runningPushAnimations.remove(view);
  104. }
  105. }
  106. @RestrictTo(RestrictTo.Scope.TESTS)
  107. public void endPushAnimation(View view) {
  108. if (runningPushAnimations.containsKey(view)) {
  109. runningPushAnimations.get(view).end();
  110. }
  111. }
  112. }