ObjectAnimator change text color with repeat and reverse

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ...

        TextView notPaidText = findViewById(R.id.not_paid);
        ObjectAnimator colorAnim = ObjectAnimator.ofInt(
                notPaidText,
                "textColor",
                Color.parseColor("#50ff0000"),
                Color.parseColor("#00ffffff")
        );
        colorAnim.setEvaluator(new ArgbEvaluator());
        colorAnim.setDuration(10000);
        TimeInterpolator baseInterpolator = colorAnim.getInterpolator();
        colorAnim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (animation.getInterpolator() instanceof AccelerateDecelerateInterpolator) {
                    animation.setInterpolator(new ReverseInterpolator());
                } else {
                    animation.setInterpolator(baseInterpolator);
                }
                animation.start();
            }
        });
        colorAnim.start();
    }

    public class ReverseInterpolator implements Interpolator {

        private final Interpolator delegate;

        public ReverseInterpolator(Interpolator delegate) {
            this.delegate = delegate;
        }

        public ReverseInterpolator() {
            this(new LinearInterpolator());
        }

        @Override
        public float getInterpolation(float input) {
            return 1 - delegate.getInterpolation(input);
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *




Enter Captcha Here :