react-native-navigation的迁移库

VisibilityAnimator.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.reactnativenavigation.animation;
  2. import android.animation.Animator;
  3. import android.animation.AnimatorListenerAdapter;
  4. import android.animation.ObjectAnimator;
  5. import android.support.v4.view.animation.LinearOutSlowInInterpolator;
  6. import android.view.View;
  7. import com.reactnativenavigation.views.ScrollDirectionListener;
  8. public class VisibilityAnimator {
  9. public enum HideDirection {
  10. Up, Down
  11. }
  12. private enum VisibilityState {
  13. Hidden, AnimateHide, Shown, AnimateShow
  14. }
  15. private static final int SHOW_END_VALUE = 0;
  16. private static final int DURATION = 300;
  17. private final View view;
  18. private final HideDirection hideDirection;
  19. private final int hiddenEndValue;
  20. private VisibilityState visibilityState = VisibilityState.Shown;
  21. public VisibilityAnimator(View view, HideDirection hideDirection, int height) {
  22. this.view = view;
  23. this.hideDirection = hideDirection;
  24. this.hiddenEndValue = hideDirection == HideDirection.Up ? -height : height;
  25. }
  26. public void onScrollChanged(ScrollDirectionListener.Direction scrollDirection) {
  27. if (hideDirection == HideDirection.Down) {
  28. handleDownHidingViews(scrollDirection);
  29. } else {
  30. handleUpHidingViews(scrollDirection);
  31. }
  32. }
  33. public void setVisible(boolean visible, boolean animate) {
  34. if (visible && isHiding()) {
  35. show(animate);
  36. } else if (!visible && isShowing()) {
  37. hide(animate);
  38. }
  39. }
  40. private void handleUpHidingViews(ScrollDirectionListener.Direction scrollDirection) {
  41. if (scrollUp(scrollDirection) && !isShowing()) {
  42. show(true);
  43. } else if (scrollDown(scrollDirection) && !isHiding()) {
  44. hide(true);
  45. }
  46. }
  47. private void handleDownHidingViews(ScrollDirectionListener.Direction scrollDirection) {
  48. if (scrollDown(scrollDirection) && !isHiding()) {
  49. hide(true);
  50. } else if (scrollUp(scrollDirection) && !isShowing()) {
  51. show(true);
  52. }
  53. }
  54. private void show(boolean animate) {
  55. if (animate) {
  56. ObjectAnimator animator = createAnimator(true);
  57. animator.start();
  58. } else {
  59. visibilityState = VisibilityState.Shown;
  60. view.setVisibility(View.VISIBLE);
  61. }
  62. }
  63. private void hide(boolean animate) {
  64. if (animate) {
  65. ObjectAnimator animator = createAnimator(false);
  66. animator.start();
  67. } else {
  68. visibilityState = VisibilityState.Hidden;
  69. view.setVisibility(View.GONE);
  70. }
  71. }
  72. private boolean scrollUp(ScrollDirectionListener.Direction scrollDirection) {
  73. return scrollDirection == ScrollDirectionListener.Direction.Up;
  74. }
  75. private boolean scrollDown(ScrollDirectionListener.Direction scrollDirection) {
  76. return scrollDirection == ScrollDirectionListener.Direction.Down;
  77. }
  78. private boolean isShowing() {
  79. return visibilityState == VisibilityState.Shown || visibilityState == VisibilityState.AnimateShow;
  80. }
  81. private boolean isHiding() {
  82. return visibilityState == VisibilityState.Hidden || visibilityState == VisibilityState.AnimateHide;
  83. }
  84. private ObjectAnimator createAnimator(final boolean show) {
  85. ObjectAnimator animator = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, show ? SHOW_END_VALUE : hiddenEndValue);
  86. animator.setDuration(DURATION);
  87. animator.setInterpolator(new LinearOutSlowInInterpolator());
  88. animator.addListener(new AnimatorListenerAdapter() {
  89. @Override
  90. public void onAnimationStart(Animator animation) {
  91. visibilityState = show ? VisibilityState.AnimateShow : VisibilityState.AnimateHide;
  92. }
  93. @Override
  94. public void onAnimationEnd(Animator animation) {
  95. visibilityState = show ? VisibilityState.Shown : VisibilityState.Hidden;
  96. }
  97. });
  98. return animator;
  99. }
  100. }