react-native-navigation的迁移库

FloatingActionButtonAnimator.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.reactnativenavigation.views;
  2. import android.animation.Animator;
  3. import android.animation.AnimatorListenerAdapter;
  4. import android.support.design.widget.FloatingActionButton;
  5. import android.view.View;
  6. import java.util.List;
  7. public class FloatingActionButtonAnimator {
  8. private final FloatingActionButton collapsedFab;
  9. private final FloatingActionButton expendedFab;
  10. private int crossFadeAnimationDuration;
  11. private enum State{Showing, Idle, Removing}
  12. private State state = State.Idle;
  13. public FloatingActionButtonAnimator(FloatingActionButton collapsedFab, FloatingActionButton expendedFab, int crossFadeAnimationDuration) {
  14. this.collapsedFab = collapsedFab;
  15. this.expendedFab = expendedFab;
  16. this.crossFadeAnimationDuration = crossFadeAnimationDuration;
  17. }
  18. boolean isAnimating() {
  19. return state == State.Showing || state == State.Removing;
  20. }
  21. void show() {
  22. state = State.Showing;
  23. collapsedFab.setScaleX(0);
  24. collapsedFab.setScaleY(0);
  25. collapsedFab.animate()
  26. .alpha(1)
  27. .scaleX(1)
  28. .scaleY(1)
  29. .setListener(new AnimatorListenerAdapter() {
  30. @Override
  31. public void onAnimationEnd(Animator animation) {
  32. state = State.Idle;
  33. }
  34. })
  35. .setDuration(crossFadeAnimationDuration)
  36. .start();
  37. }
  38. public void collapse() {
  39. hideExpended();
  40. showCollapsed();
  41. }
  42. void hideCollapsed() {
  43. animateFab(collapsedFab, 0, 90);
  44. }
  45. void showExpended() {
  46. animateFab(expendedFab, 1, 0);
  47. }
  48. void showCollapsed() {
  49. animateFab(collapsedFab, 1, 0);
  50. }
  51. void hideExpended() {
  52. animateFab(expendedFab, 0, -90);
  53. }
  54. private void animateFab(final FloatingActionButton fab, final int alpha, int rotation) {
  55. fab.animate()
  56. .alpha(alpha)
  57. .setDuration(crossFadeAnimationDuration)
  58. .rotation(rotation)
  59. .setListener(new AnimatorListenerAdapter() {
  60. @Override
  61. public void onAnimationStart(Animator animation) {
  62. if (fab.getVisibility() == View.GONE) {
  63. fab.setVisibility(View.VISIBLE);
  64. }
  65. }
  66. @Override
  67. public void onAnimationEnd(Animator animation) {
  68. fab.setVisibility(alpha == 0 ? View.GONE : View.VISIBLE);
  69. }
  70. })
  71. .start();
  72. }
  73. void removeFabFromScreen(FloatingActionButton fab, final AnimatorListenerAdapter animationListener) {
  74. if (fab == null) {
  75. return;
  76. }
  77. state = State.Removing;
  78. fab.animate()
  79. .alpha(0)
  80. .scaleX(0)
  81. .scaleY(0)
  82. .setDuration(crossFadeAnimationDuration)
  83. .setListener(new AnimatorListenerAdapter() {
  84. @Override
  85. public void onAnimationEnd(Animator animation) {
  86. if (animationListener != null) {
  87. animationListener.onAnimationEnd(animation);
  88. }
  89. }
  90. })
  91. .start();
  92. }
  93. void removeActionsFromScreen(List<FloatingActionButton> actions) {
  94. for (FloatingActionButton action : actions) {
  95. action.animate()
  96. .alpha(0)
  97. .scaleX(0)
  98. .scaleY(0)
  99. .setDuration(crossFadeAnimationDuration)
  100. .start();
  101. }
  102. }
  103. }