| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package com.atmob.task.view;
- import android.animation.Animator;
- import android.animation.AnimatorListenerAdapter;
- import android.animation.ValueAnimator;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.util.AttributeSet;
- import android.view.View;
- import androidx.annotation.NonNull;
- import androidx.annotation.Nullable;
- import androidx.recyclerview.widget.RecyclerView;
- public class TwinklingRecyclerView extends RecyclerView {
- private Paint paint;
- private boolean twinkling;
- private ValueAnimator valueAnimator;
- public TwinklingRecyclerView(@NonNull Context context) {
- this(context, null);
- }
- public TwinklingRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
- this(context, attrs, 0);
- }
- public TwinklingRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- init();
- }
- private void init() {
- paint = new Paint(Paint.ANTI_ALIAS_FLAG);
- paint.setColor(Color.parseColor("#ccff7c7c"));
- addItemDecoration(new ItemDecoration() {
- @Override
- public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull State state) {
- super.onDrawOver(c, parent, state);
- if (!twinkling) {
- return;
- }
- for (int i = 0; i < parent.getChildCount(); i++) {
- View childAt = parent.getChildAt(i);
- int left = childAt.getLeft();
- int top = childAt.getTop();
- int right = childAt.getRight();
- int bottom = childAt.getBottom();
- c.drawRoundRect(left, top, right, bottom, 10, 10, paint);
- }
- }
- });
- }
- public void twinkling() {
- if (valueAnimator == null) {
- valueAnimator = new ValueAnimator();
- valueAnimator.setFloatValues(0, 1);
- valueAnimator.setRepeatCount(2);
- valueAnimator.setRepeatMode(ValueAnimator.RESTART);
- valueAnimator.setDuration(500);
- valueAnimator.addUpdateListener(animation -> {
- float value = (float) animation.getAnimatedValue();
- twinkling = value > 0.5;
- invalidate();
- });
- valueAnimator.addListener(new AnimatorListenerAdapter() {
- @Override
- public void onAnimationEnd(Animator animation) {
- super.onAnimationEnd(animation);
- twinkling = false;
- invalidate();
- }
- });
- }
- if (valueAnimator.isRunning()) {
- valueAnimator.cancel();
- }
- valueAnimator.start();
- }
- @Override
- protected void onDetachedFromWindow() {
- super.onDetachedFromWindow();
- if (valueAnimator != null) {
- valueAnimator.cancel();
- }
- }
- }
|