react-native-navigation的迁移库

ScreenAnimator.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package com.reactnativenavigation.screens;
  2. import android.animation.Animator;
  3. import android.animation.AnimatorListenerAdapter;
  4. import android.animation.AnimatorSet;
  5. import android.animation.ObjectAnimator;
  6. import android.view.View;
  7. import android.view.animation.AccelerateInterpolator;
  8. import android.view.animation.DecelerateInterpolator;
  9. import android.view.animation.LinearInterpolator;
  10. import com.reactnativenavigation.NavigationApplication;
  11. import com.reactnativenavigation.utils.ViewUtils;
  12. import javax.annotation.Nullable;
  13. public class ScreenAnimator {
  14. private final float translationY;
  15. private Screen screen;
  16. public ScreenAnimator(Screen screen) {
  17. this.screen = screen;
  18. translationY = 0.08f * ViewUtils.getScreenHeight();
  19. }
  20. public void show(boolean animate, final Runnable onAnimationEnd) {
  21. if (animate) {
  22. createShowAnimator(onAnimationEnd).start();
  23. } else {
  24. screen.setVisibility(View.VISIBLE);
  25. NavigationApplication.instance.runOnMainThread(onAnimationEnd, 200);
  26. }
  27. }
  28. public void show(boolean animate) {
  29. if (animate) {
  30. createShowAnimator(null).start();
  31. } else {
  32. screen.setVisibility(View.VISIBLE);
  33. }
  34. }
  35. public void hide(boolean animate, Runnable onAnimationEnd) {
  36. if (animate) {
  37. createHideAnimator(onAnimationEnd).start();
  38. } else {
  39. screen.setVisibility(View.INVISIBLE);
  40. onAnimationEnd.run();
  41. }
  42. }
  43. private Animator createShowAnimator(final @Nullable Runnable onAnimationEnd) {
  44. ObjectAnimator alpha = ObjectAnimator.ofFloat(screen, View.ALPHA, 0, 1);
  45. alpha.setInterpolator(new DecelerateInterpolator());
  46. alpha.setDuration(200);
  47. ObjectAnimator translationY = ObjectAnimator.ofFloat(screen, View.TRANSLATION_Y, this.translationY, 0);
  48. translationY.setInterpolator(new DecelerateInterpolator());
  49. translationY.setDuration(280);
  50. AnimatorSet set = new AnimatorSet();
  51. set.playTogether(translationY, alpha);
  52. set.addListener(new AnimatorListenerAdapter() {
  53. @Override
  54. public void onAnimationStart(Animator animation) {
  55. screen.setVisibility(View.VISIBLE);
  56. }
  57. @Override
  58. public void onAnimationEnd(Animator animation) {
  59. if (onAnimationEnd != null) {
  60. onAnimationEnd.run();
  61. }
  62. }
  63. });
  64. return set;
  65. }
  66. private Animator createHideAnimator(final Runnable onAnimationEnd) {
  67. ObjectAnimator alpha = ObjectAnimator.ofFloat(screen, View.ALPHA, 0);
  68. alpha.setInterpolator(new LinearInterpolator());
  69. alpha.setStartDelay(100);
  70. alpha.setDuration(150);
  71. ObjectAnimator translationY = ObjectAnimator.ofFloat(screen, View.TRANSLATION_Y, this.translationY);
  72. translationY.setInterpolator(new AccelerateInterpolator());
  73. translationY.setDuration(250);
  74. AnimatorSet set = new AnimatorSet();
  75. set.playTogether(translationY, alpha);
  76. set.addListener(new AnimatorListenerAdapter() {
  77. @Override
  78. public void onAnimationEnd(Animator animation) {
  79. onAnimationEnd.run();
  80. }
  81. });
  82. return set;
  83. }
  84. }