TwinklingRecyclerView.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package com.atmob.task.view;
  2. import android.animation.Animator;
  3. import android.animation.AnimatorListenerAdapter;
  4. import android.animation.ValueAnimator;
  5. import android.content.Context;
  6. import android.graphics.Canvas;
  7. import android.graphics.Color;
  8. import android.graphics.Paint;
  9. import android.util.AttributeSet;
  10. import android.view.View;
  11. import androidx.annotation.NonNull;
  12. import androidx.annotation.Nullable;
  13. import androidx.recyclerview.widget.RecyclerView;
  14. public class TwinklingRecyclerView extends RecyclerView {
  15. private Paint paint;
  16. private boolean twinkling;
  17. private ValueAnimator valueAnimator;
  18. public TwinklingRecyclerView(@NonNull Context context) {
  19. this(context, null);
  20. }
  21. public TwinklingRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
  22. this(context, attrs, 0);
  23. }
  24. public TwinklingRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  25. super(context, attrs, defStyleAttr);
  26. init();
  27. }
  28. private void init() {
  29. paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  30. paint.setColor(Color.parseColor("#ccff7c7c"));
  31. addItemDecoration(new ItemDecoration() {
  32. @Override
  33. public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull State state) {
  34. super.onDrawOver(c, parent, state);
  35. if (!twinkling) {
  36. return;
  37. }
  38. for (int i = 0; i < parent.getChildCount(); i++) {
  39. View childAt = parent.getChildAt(i);
  40. int left = childAt.getLeft();
  41. int top = childAt.getTop();
  42. int right = childAt.getRight();
  43. int bottom = childAt.getBottom();
  44. c.drawRoundRect(left, top, right, bottom, 10, 10, paint);
  45. }
  46. }
  47. });
  48. }
  49. public void twinkling() {
  50. if (valueAnimator == null) {
  51. valueAnimator = new ValueAnimator();
  52. valueAnimator.setFloatValues(0, 1);
  53. valueAnimator.setRepeatCount(2);
  54. valueAnimator.setRepeatMode(ValueAnimator.RESTART);
  55. valueAnimator.setDuration(500);
  56. valueAnimator.addUpdateListener(animation -> {
  57. float value = (float) animation.getAnimatedValue();
  58. twinkling = value > 0.5;
  59. invalidate();
  60. });
  61. valueAnimator.addListener(new AnimatorListenerAdapter() {
  62. @Override
  63. public void onAnimationEnd(Animator animation) {
  64. super.onAnimationEnd(animation);
  65. twinkling = false;
  66. invalidate();
  67. }
  68. });
  69. }
  70. if (valueAnimator.isRunning()) {
  71. valueAnimator.cancel();
  72. }
  73. valueAnimator.start();
  74. }
  75. @Override
  76. protected void onDetachedFromWindow() {
  77. super.onDetachedFromWindow();
  78. if (valueAnimator != null) {
  79. valueAnimator.cancel();
  80. }
  81. }
  82. }