Badr Bujbara
Badr Bujbara

Reputation: 8671

Android: ValueAnimator Flashes Colors So Quickly?

I'm using the property animator ValueAnimator to animate logo color change. The animation works, but the colors flash so quickly. How do I control them and make the colors change slowly and smoothly?

Here is the code I'm using:

    private void animateLogo(){ 
    ImageView imgLogo = findViewById(R.id.imgview_logo);
    ValueAnimator animator = ValueAnimator.ofArgb();
    animator.setDuration(5000);
    animator.setEvaluator(new ArgbEvaluator());
    animator.setIntValues(R.color.colorGoldNew,
            R.color.dbIndex0,
            R.color.dbIndex5,
            R.color.dbIndex6);
    animator.addUpdateListener(animation ->

            imgLogo.setColorFilter(getResources().getColor((int) animator.getAnimatedValue()))
    );
    animator.start(); 
}

Upvotes: 1

Views: 218

Answers (1)

Kalai
Kalai

Reputation: 543

The ArgbEvaluator evaluates based on 32-bit int value representing colors. You are currently using R class ID int values which I suspect is messing up the evaluator.

I have used the respective int color value by using getColor()

    private void animateLogo(){
    ImageView imgLogo = findViewById(R.id.imageView);
    ValueAnimator colorAnim = ValueAnimator.ofInt(getColor(R.color.colorGoldNew),getColor(R.color.dbIndex0),getColor(R.color.dbIndex5),getColor(R.color.dbIndex6));
    colorAnim.setDuration(5000);
    colorAnim.setEvaluator(new ArgbEvaluator());
    colorAnim.addUpdateListener(valueAnimator -> imgLogo.setColorFilter((int)colorAnim.getAnimatedValue()) );
    colorAnim.start();
}

Result:

enter image description here

Upvotes: 3

Related Questions