react-native-navigation的迁移库

Scrim.java 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package com.reactnativenavigation.views;
  2. import android.content.Context;
  3. import android.view.View;
  4. import android.view.animation.DecelerateInterpolator;
  5. import android.view.animation.Interpolator;
  6. import com.reactnativenavigation.params.StyleParams;
  7. import static com.reactnativenavigation.views.Scrim.State.Invisible;
  8. import static com.reactnativenavigation.views.Scrim.State.Visible;
  9. public class Scrim extends View {
  10. enum State {Visible, Invisible}
  11. private State state = Invisible;
  12. private final float collapseThreshold;
  13. private final static int ANIMATION_DURATION = 600;
  14. private final Interpolator interpolator;
  15. public Scrim(Context context, StyleParams.Color color, float collapseThreshold) {
  16. super(context);
  17. this.collapseThreshold = collapseThreshold;
  18. setBackgroundColor(color.getColor());
  19. setAlpha(0);
  20. interpolator = new DecelerateInterpolator();
  21. }
  22. public void collapse(float collapse) {
  23. if (shouldShowScrim(collapse)) {
  24. showScrim();
  25. } else if (shouldHideScrim(collapse)) {
  26. hideScrim();
  27. }
  28. }
  29. private boolean shouldShowScrim(float collapse) {
  30. return Math.abs(collapse) >= collapseThreshold && state == Invisible;
  31. }
  32. private boolean shouldHideScrim(float collapse) {
  33. return Math.abs(collapse) < collapseThreshold && state == Visible;
  34. }
  35. private void showScrim() {
  36. state = Visible;
  37. animate()
  38. .alpha(1)
  39. .setDuration(ANIMATION_DURATION)
  40. .setInterpolator(interpolator);
  41. }
  42. private void hideScrim() {
  43. state = Invisible;
  44. animate()
  45. .alpha(0)
  46. .setDuration(ANIMATION_DURATION)
  47. .setInterpolator(interpolator);
  48. }
  49. }