Animate change of view background color on Android
Asked Answered
D

18

392

How do you animate the change of background color of a view on Android?

For example:

I have a view with a red background color. The background color of the view changes to blue. How can I do a smooth transition between colors?

If this can't be done with views, an alternative will be welcome.

Dress answered 10/4, 2010 at 18:58 Comment(2)
One would expect that a smooth transition between colors shouldn't be a problem for a platform like Android. Maybe views aren't built for it, though. The question still stands.Dress
A good video tutorialCircumstantial
S
427

I ended up figuring out a (pretty good) solution for this problem!

You can use a TransitionDrawable to accomplish this. For example, in an XML file in the drawable folder you could write something like:

<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- The drawables used here can be solid colors, gradients, shapes, images, etc. -->
    <item android:drawable="@drawable/original_state" />
    <item android:drawable="@drawable/new_state" />
</transition>

Then, in your XML for the actual View you would reference this TransitionDrawable in the android:background attribute.

At this point you can initiate the transition in your code on-command by doing:

TransitionDrawable transition = (TransitionDrawable) viewObj.getBackground();
transition.startTransition(transitionTime);

Or run the transition in reverse by calling:

transition.reverseTransition(transitionTime);

See Roman's answer for another solution using the Property Animation API, which wasn't available at the time this answer was originally posted.

Stroh answered 8/7, 2010 at 23:29 Comment(12)
Thanks mxrider! This does answer the question. Unfortunately it doesn't work for me because the view that has the TransitionDrawable as background has to be animated as well, and it seems that the view animation overrides the background transition. Any ideas?Dress
Solved it by starting the TransitionDrawable AFTER starting the animation.Dress
Awesome! Glad you got it working! There is a lot of cool stuff you can do in xml with Android--much of which is kinda buried in the docs and not obvious in my opinion.Stroh
Not obvious at all. I should mention that I'm not using xml in my case, as the color is dynamic. Your solution also works if you create the TransitionDrawable by code.Dress
Is it possible to use this method in a looping or auto-reversing like UIView animations on iOS?Basile
It does not work smoothly cross-fade as I expected. using 4.1.2 device.Pathfinder
How about for a textview background animation from previous color to the next?Asbestos
I had an xml as a drawable and wanted to change to another xml. This method works in such case.Boaster
@Stroh i want this effect continuously . i tried it with a timer task . but it is not smooth. what am i suppose to doPoverty
One of the main drawbacks is that there is no animation complete listenerBelton
is it possible to animate more than 2 colors?Hydrolyse
Thanks a lot. I waste 5 hours to find this solutionCricket
U
563

You can use new Property Animation Api for color animation:

int colorFrom = getResources().getColor(R.color.red);
int colorTo = getResources().getColor(R.color.blue);
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.setDuration(250); // milliseconds
colorAnimation.addUpdateListener(new AnimatorUpdateListener() {

    @Override
    public void onAnimationUpdate(ValueAnimator animator) {
        textView.setBackgroundColor((int) animator.getAnimatedValue());
    }

});
colorAnimation.start();

For backward compatibility with Android 2.x use Nine Old Androids library from Jake Wharton.

The getColor method was deprecated in Android M, so you have two choices:

  • If you use the support library, you need to replace the getColor calls with:

    ContextCompat.getColor(this, R.color.red);
    
  • if you don't use the support library, you need to replace the getColor calls with:

    getColor(R.color.red);
    
Unship answered 22/1, 2013 at 20:42 Comment(10)
"musthave" solution if you are using Property Animation API (or NineOldAndroid). Animates very smoothly.Ethyl
Is there any way to set the duration of the transition in this way?Kaffir
@Kaffir yes, you are not not gonna believe it but the method call setDuration() :)Unship
can i do this with drawables?Backboard
ValueAnimator.ofArgb(colorFrom, colorTo) looks more to the point, but sadly it's new.Nerta
To get current background color of layout, use this code. ((ColorDrawable)frame_layout.getBackground()).getColor()Taproot
ArgbEvaluator is now private, don't use it. Legally reasons.Access
@PedroPauloAmorim why do you think it's now private? Documentation does not agree with you.Unship
ArgbEvaluator from android.animation is not restricted, but the same class of android.support.graphics.drawable is tagged as @RestrictTo(LIBRARY_GROUP). It generates a warning.Access
i want color change animation like growing from bottom to top , how can i achieve thatTiros
S
427

I ended up figuring out a (pretty good) solution for this problem!

You can use a TransitionDrawable to accomplish this. For example, in an XML file in the drawable folder you could write something like:

<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- The drawables used here can be solid colors, gradients, shapes, images, etc. -->
    <item android:drawable="@drawable/original_state" />
    <item android:drawable="@drawable/new_state" />
</transition>

Then, in your XML for the actual View you would reference this TransitionDrawable in the android:background attribute.

At this point you can initiate the transition in your code on-command by doing:

TransitionDrawable transition = (TransitionDrawable) viewObj.getBackground();
transition.startTransition(transitionTime);

Or run the transition in reverse by calling:

transition.reverseTransition(transitionTime);

See Roman's answer for another solution using the Property Animation API, which wasn't available at the time this answer was originally posted.

Stroh answered 8/7, 2010 at 23:29 Comment(12)
Thanks mxrider! This does answer the question. Unfortunately it doesn't work for me because the view that has the TransitionDrawable as background has to be animated as well, and it seems that the view animation overrides the background transition. Any ideas?Dress
Solved it by starting the TransitionDrawable AFTER starting the animation.Dress
Awesome! Glad you got it working! There is a lot of cool stuff you can do in xml with Android--much of which is kinda buried in the docs and not obvious in my opinion.Stroh
Not obvious at all. I should mention that I'm not using xml in my case, as the color is dynamic. Your solution also works if you create the TransitionDrawable by code.Dress
Is it possible to use this method in a looping or auto-reversing like UIView animations on iOS?Basile
It does not work smoothly cross-fade as I expected. using 4.1.2 device.Pathfinder
How about for a textview background animation from previous color to the next?Asbestos
I had an xml as a drawable and wanted to change to another xml. This method works in such case.Boaster
@Stroh i want this effect continuously . i tried it with a timer task . but it is not smooth. what am i suppose to doPoverty
One of the main drawbacks is that there is no animation complete listenerBelton
is it possible to animate more than 2 colors?Hydrolyse
Thanks a lot. I waste 5 hours to find this solutionCricket
D
150

Depending on how your view gets its background color and how you get your target color there are several different ways to do this.

The first two uses the Android Property Animation framework.

Use a Object Animator if:

  • Your view have its background color defined as a argb value in a xml file.
  • Your view have previously had its color set by view.setBackgroundColor()
  • Your view have its background color defined in a drawable that DOES NOT defines any extra properties like stroke or corner radiuses.
  • Your view have its background color defined in a drawable and you want to remove any extra properties like stroke or corner radiuses, keep in mind that the removal of the extra properties will not animated.

The object animator works by calling view.setBackgroundColor which replaces the defined drawable unless is it an instance of a ColorDrawable, which it rarely is. This means that any extra background properties from a drawable like stroke or corners will be removed.

Use a Value Animator if:

  • Your view have its background color defined in a drawable that also sets properties like the stroke or corner radiuses AND you want to change it to a new color that is decided while running.

Use a Transition drawable if:

  • Your view should switch between two drawable that have been defined before deployment.

I have had some performance issues with Transition drawables that runs while I am opening a DrawerLayout that I haven't been able to solve, so if you encounter any unexpected stuttering you might have run into the same bug as I have.

You will have to modify the Value Animator example if you want to use a StateLists drawable or a LayerLists drawable, otherwise it will crash on the final GradientDrawable background = (GradientDrawable) view.getBackground(); line.

Object Animator:

View definition:

<View
    android:background="#FFFF0000"
    android:layout_width="50dp"
    android:layout_height="50dp"/>

Create and use a ObjectAnimator like this.

final ObjectAnimator backgroundColorAnimator = ObjectAnimator.ofObject(view,
                                                                       "backgroundColor",
                                                                       new ArgbEvaluator(),
                                                                       0xFFFFFFFF,
                                                                       0xff78c5f9);
backgroundColorAnimator.setDuration(300);
backgroundColorAnimator.start();

You can also load the animation definition from a xml using a AnimatorInflater like XMight does in Android objectAnimator animate backgroundColor of Layout

Value Animator:

View definition:

<View
    android:background="@drawable/example"
    android:layout_width="50dp"
    android:layout_height="50dp"/>

Drawable definition:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF"/>
    <stroke
        android:color="#edf0f6"
        android:width="1dp"/>
    <corners android:radius="3dp"/>

</shape>

Create and use a ValueAnimator like this:

final ValueAnimator valueAnimator = ValueAnimator.ofObject(new ArgbEvaluator(),
                                                           0xFFFFFFFF,
                                                           0xff78c5f9);

final GradientDrawable background = (GradientDrawable) view.getBackground();
currentAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

    @Override
    public void onAnimationUpdate(final ValueAnimator animator) {
        background.setColor((Integer) animator.getAnimatedValue());
    }

});
currentAnimation.setDuration(300);
currentAnimation.start();

Transition drawable:

View definition:

<View
    android:background="@drawable/example"
    android:layout_width="50dp"
    android:layout_height="50dp"/>

Drawable definition:

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="#FFFFFF"/>
            <stroke
                android:color="#edf0f6"
                android:width="1dp"/>
            <corners android:radius="3dp"/>
        </shape>
    </item>

    <item>
        <shape>
            <solid android:color="#78c5f9"/>
            <stroke
                android:color="#68aff4"
                android:width="1dp"/>
            <corners android:radius="3dp"/>
        </shape>
    </item>
</transition>

Use the TransitionDrawable like this:

final TransitionDrawable background = (TransitionDrawable) view.getBackground();
background.startTransition(300);

You can reverse the animations by calling .reverse() on the animation instance.

There are some other ways to do animations but these three is probably the most common. I generally use a ValueAnimator.

Diarchy answered 10/9, 2013 at 15:36 Comment(2)
actually it is possible to define and use the TransitionDrawable in code, but for some reason, it doesn't work the second time. weird.Linwoodlinz
I am able to use the object animator even when my view has ColorDrawable set. I just save it to a local variable, set it to null and when the animation is finished, set the original ColorDrawable back.Moniliform
U
61

You can make an object animator. For example, I have a targetView and I want to change your background color:

int colorFrom = Color.RED;
int colorTo = Color.GREEN;
int duration = 1000;
ObjectAnimator.ofObject(targetView, "backgroundColor", new ArgbEvaluator(), colorFrom, colorTo)
    .setDuration(duration)
    .start();
Urbanize answered 5/9, 2014 at 20:17 Comment(1)
From API 21, you can use ofArgb(targetView, "backgroundColor", colorFrom, colorTo). Its implementation is just ofInt(...).setEvaluator(new ArgbEvaluator()).Smaltite
G
33

If you want color animation like this,

Enter image description here

this code will help you:

ValueAnimator anim = ValueAnimator.ofFloat(0, 1);   
anim.setDuration(2000);

float[] hsv;
int runColor;
int hue = 0;
hsv = new float[3]; // Transition color
hsv[1] = 1;
hsv[2] = 1;
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {

        hsv[0] = 360 * animation.getAnimatedFraction();

        runColor = Color.HSVToColor(hsv);
        yourView.setBackgroundColor(runColor);
    }
});

anim.setRepeatCount(Animation.INFINITE);

anim.start();
Greathearted answered 23/12, 2015 at 6:3 Comment(5)
Where is the part when anim variable is initialized?Taproot
@Taproot , it's just instance of ValueAnimator.Greathearted
It should be anim.setRepeatCount(ValueAnimator.INFINITE); not Animation.INFINITE for now it's the same, but we have no guaranteesDermato
@Dermato what's wrong with Animation.INFINITE? developer.android.com/reference/android/view/animation/…Greathearted
is it possible to set as button background?Rowdyish
H
21

best way is to use ValueAnimator and ColorUtils.blendARGB

 ValueAnimator valueAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);
 valueAnimator.setDuration(325);
 valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {

              float fractionAnim = (float) valueAnimator.getAnimatedValue();

              view.setBackgroundColor(ColorUtils.blendARGB(Color.parseColor("#FFFFFF")
                                    , Color.parseColor("#000000")
                                    , fractionAnim));
        }
});
valueAnimator.start();
Homes answered 24/10, 2017 at 12:37 Comment(3)
This solution is the best for multiple colors views animations with the same ValueAnimator Listener.Grano
Set valueAnimator.repeatCount = ValueAnimator.INFINITE if you want repetition (for kotlin solution)Facsimile
it work more smoothCaribbean
W
21

The documentation on XML powered animations is horrible. I've searched around hours just to animate the background color of a button when pressing... The sad thing is that the animation is only one attribute away: You can use exitFadeDuration in the selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:exitFadeDuration="200">

    <item android:state_pressed="true">
        <shape android:tint="#3F51B5" />
    </item>

    <item>
        <shape android:tint="#F44336" />
    </item>

</selector>

Then just use it as background for your view. No Java/Kotlin code needed.

Watermark answered 4/10, 2019 at 14:58 Comment(2)
Excellent answer. Note that you may need android:enterFadeDuration as well; my experience was that, without it, my animation didn't work the second time.Ladder
@String, strange, on an emulator it works many times without android:enterFadeDuration.Kilohertz
A
13

Another easy way to achieve this is to perform a fade using AlphaAnimation.

  1. Make your view a ViewGroup
  2. Add a child view to it at index 0, with match_parent layout dimensions
  3. Give your child the same background as the container
  4. Change to background of the container to the target color
  5. Fade out the child using AlphaAnimation.
  6. Remove the child when the animation is complete (using an AnimationListener)
Alexis answered 26/4, 2012 at 13:8 Comment(0)
L
11

Here's a nice function that allows this:

public static void animateBetweenColors(final @NonNull View viewToAnimateItsBackground, final int colorFrom,
                                        final int colorTo, final int durationInMs) {
    final ColorDrawable colorDrawable = new ColorDrawable(durationInMs > 0 ? colorFrom : colorTo);
    ViewCompat.setBackground(viewToAnimateItsBackground, colorDrawable);
    if (durationInMs > 0) {
        final ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
        colorAnimation.addUpdateListener(animator -> {
            colorDrawable.setColor((Integer) animator.getAnimatedValue());
            ViewCompat.setBackground(viewToAnimateItsBackground, colorDrawable);
        });
        colorAnimation.setDuration(durationInMs);
        colorAnimation.start();
    }
}

And in Kotlin:

@JvmStatic
fun animateBetweenColors(viewToAnimateItsBackground: View, colorFrom: Int, colorTo: Int, durationInMs: Int) {
    val colorDrawable = ColorDrawable(if (durationInMs > 0) colorFrom else colorTo)
    ViewCompat.setBackground(viewToAnimateItsBackground, colorDrawable)
    if (durationInMs > 0) {
        val colorAnimation = ValueAnimator.ofObject(ArgbEvaluator(), colorFrom, colorTo)
        colorAnimation.addUpdateListener { animator: ValueAnimator ->
            colorDrawable.color = (animator.animatedValue as Int)
            ViewCompat.setBackground(viewToAnimateItsBackground, colorDrawable)
        }
        colorAnimation.duration = durationInMs.toLong()
        colorAnimation.start()
    }
}
Linwoodlinz answered 30/4, 2014 at 8:10 Comment(3)
this doesnt support lower end version phonesPenance
@Kartheeks you mean Android 10 and below? if so, you can easily use a "nineOldAndroids" library to support it: nineoldandroids.com . the syntax is almost exactly the same.Linwoodlinz
for lower version colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { colorDrawable.setColor((Integer) animation.getAnimatedValue()); ViewCompat.setBackground(viewToAnimateItsBackground, colorDrawable); } });Ipecac
O
10

This is the method I use in a Base Activity to change background. I'm using GradientDrawables generated in code, but could be adapted to suit.

    protected void setPageBackground(View root, int type){
        if (root!=null) {
            Drawable currentBG = root.getBackground();
            //add your own logic here to determine the newBG 
            Drawable newBG = Utils.createGradientDrawable(type); 
            if (currentBG==null) {
                if(Build.VERSION.SDK_INT<Build.VERSION_CODES.JELLY_BEAN){
                    root.setBackgroundDrawable(newBG);
                }else{
                    root.setBackground(newBG);
                }
            }else{
                TransitionDrawable transitionDrawable = new TransitionDrawable(new Drawable[]{currentBG, newBG});
                transitionDrawable.setCrossFadeEnabled(true);
                if(Build.VERSION.SDK_INT<Build.VERSION_CODES.JELLY_BEAN){
                     root.setBackgroundDrawable(transitionDrawable);
                }else{
                    root.setBackground(transitionDrawable);
                }
                transitionDrawable.startTransition(400);
            }
        }
    }

update: In case anyone runs in to same issue I found, for some reason on Android <4.3 using setCrossFadeEnabled(true) cause a undesirable white out effect so I had to switch to a solid colour for <4.3 using @Roman Minenok ValueAnimator method noted above.

Oboe answered 9/7, 2014 at 11:40 Comment(0)
H
9

Answer is given in many ways. You can also use ofArgb(startColor,endColor) of ValueAnimator.

for API > 21:

int cyanColorBg = ContextCompat.getColor(this,R.color.cyan_bg);
int purpleColorBg = ContextCompat.getColor(this,R.color.purple_bg);

ValueAnimator valueAnimator = ValueAnimator.ofArgb(cyanColorBg,purpleColorBg);
        valueAnimator.setDuration(500);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
              @Override
              public void onAnimationUpdate(ValueAnimator valueAnimator) {
                   relativeLayout.setBackgroundColor((Integer)valueAnimator.getAnimatedValue());
              }
        });
        valueAnimator.start();
Harquebus answered 3/2, 2018 at 4:6 Comment(0)
I
9

Use the below function for Kotlin:

private fun animateColorValue(view: View) {
    val colorAnimation =
        ValueAnimator.ofObject(ArgbEvaluator(), Color.GRAY, Color.CYAN)
    colorAnimation.duration = 500L
    colorAnimation.addUpdateListener { animator -> view.setBackgroundColor(animator.animatedValue as Int) }
    colorAnimation.start()
}

Pass whatever view you want to change color of.

Irrefutable answered 17/2, 2020 at 10:52 Comment(0)
C
8

Roman Minenok answer in kotlin and as an extension function

fun View.colorTransition(@ColorRes startColor: Int, @ColorRes endColor: Int, duration: Long = 250L){
    val colorFrom = ContextCompat.getColor(context, startColor)
    val colorTo =  ContextCompat.getColor(context, endColor)
    val colorAnimation: ValueAnimator = ValueAnimator.ofObject(ArgbEvaluator(), colorFrom, colorTo)
    colorAnimation.duration = duration

    colorAnimation.addUpdateListener {
        if (it.animatedValue is Int) {
            val color=it.animatedValue as Int
            setBackgroundColor(color)
        }
    }
    colorAnimation.start()
}

If you want to change from current background color to new color then you can use this

fun View.colorTransition(@ColorRes endColor: Int, duration: Long = 250L){
    var colorFrom = Color.TRANSPARENT
    if (background is ColorDrawable)
        colorFrom = (background as ColorDrawable).color

    val colorTo =  ContextCompat.getcolor(context, endColor)
    val colorAnimation: ValueAnimator = ValueAnimator.ofObject(ArgbEvaluator(), colorFrom, colorTo)
    colorAnimation.duration = duration

    colorAnimation.addUpdateListener {
        if (it.animatedValue is Int) {
            val color=it.animatedValue as Int
            setBackgroundColor(color)
        }
    }
    colorAnimation.start()
}

Usage

myView.colorTransition(R.color.bg_color)
Chongchoo answered 2/3, 2021 at 16:5 Comment(0)
W
8

You can use ValueAnimator like this:

 fun startColorAnimation(v: View) {
    val colorStart = v.solidColor
    val colorEnd = Color.RED
    val colorAnim: ValueAnimator = ObjectAnimator.ofInt(v, "backgroundColor", colorStart, colorEnd)
    colorAnim.setDuration(1000)
    colorAnim.setEvaluator(ArgbEvaluator())
    colorAnim.repeatCount = 1
    colorAnim.repeatMode = ValueAnimator.REVERSE
    colorAnim.start()
}
Wilmot answered 9/3, 2021 at 6:26 Comment(0)
D
6

add a folder animator into res folder. (the name must be animator). Add an animator resource file. For example res/animator/fade.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:propertyName="backgroundColor"
        android:duration="1000"
        android:valueFrom="#000000"
        android:valueTo="#FFFFFF"
        android:startOffset="0"
        android:repeatCount="-1"
        android:repeatMode="reverse" />
</set>

Inside Activity java file, call this

View v = getWindow().getDecorView().findViewById(android.R.id.content);
AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.fade);
set.setTarget(v);
set.start();
Disorder answered 10/5, 2018 at 11:36 Comment(0)
W
5

I've found that the implementation used by ArgbEvaluator in the Android source code does best job in transitioning colors. When using HSV, depending on the two colors, the transition was jumping through too many hues for me. But this method's doesn't.

If you are trying to simply animate, use ArgbEvaluator with ValueAnimator as suggested here:

ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.addUpdateListener(new AnimatorUpdateListener() {

    @Override
    public void onAnimationUpdate(ValueAnimator animator) {
        view.setBackgroundColor((int) animator.getAnimatedValue());
    }

});
colorAnimation.start();

However, if you are like me and want to tie your transition with some user gesture or other value passed from an input, the ValueAnimator is not of much help (unless your are targeting for API 22 and above, in which case you can use the ValueAnimator.setCurrentFraction() method). When targeting below API 22, wrap the code found in ArgbEvaluator source code in your own method, as shown below:

public static int interpolateColor(float fraction, int startValue, int endValue) {
    int startA = (startValue >> 24) & 0xff;
    int startR = (startValue >> 16) & 0xff;
    int startG = (startValue >> 8) & 0xff;
    int startB = startValue & 0xff;
    int endA = (endValue >> 24) & 0xff;
    int endR = (endValue >> 16) & 0xff;
    int endG = (endValue >> 8) & 0xff;
    int endB = endValue & 0xff;
    return ((startA + (int) (fraction * (endA - startA))) << 24) |
            ((startR + (int) (fraction * (endR - startR))) << 16) |
            ((startG + (int) (fraction * (endG - startG))) << 8) |
            ((startB + (int) (fraction * (endB - startB))));
}

And use it however you wish.

Woodworth answered 16/1, 2017 at 17:55 Comment(0)
P
1

Based on ademar111190's answer, I have created this method the will pulse the background color of a view between any two colors:

private void animateBackground(View view, int colorFrom, int colorTo, int duration) {


    ObjectAnimator objectAnimator = ObjectAnimator.ofObject(view, "backgroundColor", new ArgbEvaluator(), colorFrom, colorTo);
    objectAnimator.setDuration(duration);
    //objectAnimator.setRepeatCount(Animation.INFINITE);
    objectAnimator.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            // Call this method again, but with the two colors switched around.
            animateBackground(view, colorTo, colorFrom, duration);
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });
    objectAnimator.start();
}
Printmaker answered 6/11, 2019 at 11:14 Comment(0)
R
1

You can use ArgbEvaluatorCompat class above API 11.

implementation 'com.google.android.material:material:1.0.0' 


ValueAnimator colorAnim = ValueAnimator.ofObject(new ArgbEvaluatorCompat(), startColor, endColor);
colorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            mTargetColor = (int) animation.getAnimatedValue();
        }
    });
colorAnim.start();
Rowan answered 9/9, 2020 at 2:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.