react-native-navigation的迁移库

TopBarAnimator.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package com.reactnativenavigation.anim;
  2. import android.animation.Animator;
  3. import android.animation.AnimatorListenerAdapter;
  4. import android.animation.AnimatorSet;
  5. import android.animation.ObjectAnimator;
  6. import android.animation.TimeInterpolator;
  7. import android.view.View;
  8. import android.view.animation.AccelerateDecelerateInterpolator;
  9. import android.view.animation.DecelerateInterpolator;
  10. import android.view.animation.LinearInterpolator;
  11. import com.reactnativenavigation.parse.AnimationOptions;
  12. import com.reactnativenavigation.views.topbar.TopBar;
  13. import javax.annotation.Nullable;
  14. import static android.view.View.TRANSLATION_Y;
  15. public class TopBarAnimator {
  16. private static final int DEFAULT_COLLAPSE_DURATION = 100;
  17. private static final int DURATION = 300;
  18. private static final TimeInterpolator DECELERATE = new DecelerateInterpolator();
  19. private static final TimeInterpolator LINEAR = new LinearInterpolator();
  20. private static final TimeInterpolator ACCELERATE_DECELERATE = new AccelerateDecelerateInterpolator();
  21. private TopBar topBar;
  22. private String stackId;
  23. private Animator hideAnimator;
  24. private Animator showAnimator;
  25. public TopBarAnimator(TopBar topBar) {
  26. this.topBar = topBar;
  27. }
  28. public TopBarAnimator(TopBar topBar, @Nullable String stackId) {
  29. this.topBar = topBar;
  30. this.stackId = stackId;
  31. }
  32. public void show(AnimationOptions options) {
  33. if (options.hasValue() && (!options.id.hasValue() || options.id.get().equals(stackId))) {
  34. showAnimator = options.getAnimation(topBar);
  35. } else {
  36. showAnimator = getDefaultShowAnimator(-1 * topBar.getMeasuredHeight(), DECELERATE, DURATION);
  37. }
  38. show();
  39. }
  40. public void show(float startTranslation) {
  41. showAnimator = getDefaultShowAnimator(startTranslation, LINEAR, DEFAULT_COLLAPSE_DURATION);
  42. show();
  43. }
  44. private void show() {
  45. showAnimator.addListener(new AnimatorListenerAdapter() {
  46. @Override
  47. public void onAnimationStart(Animator animation) {
  48. topBar.setVisibility(View.VISIBLE);
  49. }
  50. });
  51. topBar.resetAnimationOptions();
  52. showAnimator.start();
  53. }
  54. private AnimatorSet getDefaultShowAnimator(float startTranslation, TimeInterpolator interpolator, int duration) {
  55. ObjectAnimator showAnimator = ObjectAnimator.ofFloat(topBar, TRANSLATION_Y, startTranslation, 0);
  56. showAnimator.setInterpolator(interpolator);
  57. showAnimator.setDuration(duration);
  58. AnimatorSet set = new AnimatorSet();
  59. set.play(showAnimator);
  60. return set;
  61. }
  62. public void hide(AnimationOptions options, Runnable onAnimationEnd) {
  63. if (options.hasValue() && (!options.id.hasValue() || options.id.get().equals(stackId))) {
  64. hideAnimator = options.getAnimation(topBar);
  65. } else {
  66. hideAnimator = getDefaultHideAnimator(0, ACCELERATE_DECELERATE, DURATION);
  67. }
  68. hide(onAnimationEnd);
  69. }
  70. void hide(float startTranslation) {
  71. hideAnimator = getDefaultHideAnimator(startTranslation, LINEAR, DEFAULT_COLLAPSE_DURATION);
  72. hide(() -> {});
  73. }
  74. private void hide(Runnable onAnimationEnd) {
  75. hideAnimator.addListener(new AnimatorListenerAdapter() {
  76. @Override
  77. public void onAnimationEnd(Animator animation) {
  78. topBar.setVisibility(View.GONE);
  79. onAnimationEnd.run();
  80. }
  81. });
  82. hideAnimator.start();
  83. }
  84. private Animator getDefaultHideAnimator(float startTranslation, TimeInterpolator interpolator, int duration) {
  85. ObjectAnimator hideAnimator = ObjectAnimator.ofFloat(topBar, TRANSLATION_Y, startTranslation, -1 * topBar.getMeasuredHeight());
  86. hideAnimator.setInterpolator(interpolator);
  87. hideAnimator.setDuration(duration);
  88. return hideAnimator;
  89. }
  90. public boolean isAnimatingHide() {
  91. return hideAnimator != null && hideAnimator.isRunning();
  92. }
  93. public boolean isAnimatingShow() {
  94. return showAnimator != null && showAnimator.isRunning();
  95. }
  96. }