react-native-navigation的迁移库

ViewCollapser.java 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package com.reactnativenavigation.views.collapsingToolbar;
  2. import android.animation.Animator;
  3. import android.animation.AnimatorListenerAdapter;
  4. import android.view.ViewPropertyAnimator;
  5. public class ViewCollapser {
  6. private static final int DURATION = 160;
  7. private CollapsingView view;
  8. private ViewPropertyAnimator animator;
  9. public ViewCollapser(CollapsingView view) {
  10. this.view = view;
  11. }
  12. public void collapse(CollapseAmount amount) {
  13. if (amount.collapseToTop()) {
  14. collapseView(true, view.getFinalCollapseValue());
  15. } else if (amount.collapseToBottom()) {
  16. collapseView(true, 0);
  17. } else {
  18. collapse(amount.get());
  19. }
  20. }
  21. private void collapseView(boolean animate, float translation) {
  22. if (animate) {
  23. animate(translation);
  24. } else {
  25. collapse(translation);
  26. }
  27. }
  28. public void collapse(float amount) {
  29. if (animator != null) {
  30. animator.cancel();
  31. }
  32. view.asView().setTranslationY(amount);
  33. }
  34. private void animate(final float translation) {
  35. animator = view.asView().animate()
  36. .translationY(translation)
  37. .setDuration(DURATION)
  38. .setListener(new AnimatorListenerAdapter() {
  39. @Override
  40. public void onAnimationCancel(Animator animation) {
  41. animator = null;
  42. }
  43. @Override
  44. public void onAnimationEnd(Animator animation) {
  45. animator = null;
  46. }
  47. });
  48. animator.start();
  49. }
  50. }