15

I have a problem. I want to animate the background color of a LinearLayout, using ObjectAnimator.
The problem is that it animates, but it does neither care about duration nor valueFrom and valueTo.

This is my xml file:

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:propertyName="backgroundColor"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:valueFrom="#FF0000"
    android:valueTo="#000000" />

In Java I call like this:

ObjectAnimator objAnim = (ObjectAnimator)AnimatorInflater.loadAnimator(getActivity(), R.animator.animator_bkg);
objAnim.setTarget(view);
objAnim.start();

Note that when I animate the alpha of the layout, it works as expected.
Is this an Android bug (4.0.3 on Asus Transformer), or I miss something?

Real KEK
  • 169
  • 2
  • 12
XMight
  • 1,991
  • 2
  • 20
  • 36
  • What happens exactly when you try to animate the colors? – DeeV Jun 19 '12 at 13:01
  • The animation speed is very fast, and it looks like the background is animated with all possible colors(like a random color). I see blue, red, green, yellow, everything, and very fast :) – XMight Jun 19 '12 at 14:37
  • This fails to work because between the "number" 0xFF0000 and 0x000000 there's also the other primary colours 0x00FF00 and 0x0000FF, and pretty much every other colour as well. Set your desktop calculator to programmer/hexadecimal, and you'll see that half of red is actually dark yellow instead of dark red. – Combuster Feb 03 '14 at 14:10

2 Answers2

40

I googled a bit. There is an answer. Try to use TransitionDrawable. http://developer.android.com/guide/topics/resources/drawable-resource.html#Transition

Also, there is a topic somewhere on stackoverflow.com dedicated to the same problem.

ADDED Code example:

    Button btn = (Button)this.findViewById(R.id.btn1);
    //Let's change background's color from blue to red.
    ColorDrawable[] color = {new ColorDrawable(Color.BLUE), new ColorDrawable(Color.RED)};
    TransitionDrawable trans = new TransitionDrawable(color);
    //This will work also on old devices. The latest API says you have to use setBackground instead.
    btn.setBackgroundDrawable(trans);
    trans.startTransition(5000);
Bartek
  • 1,327
  • 1
  • 11
  • 22
user1415536
  • 236
  • 1
  • 14
  • 26
  • thank you!!!! just wanted to do a pop to new bg color on textview and fade back to black on change. tried all the other animations. even a declarative version of TransitionDrawable. this one worked ))) – tom May 12 '16 at 19:23
  • 1
    Guys, I have an issue, I did the same, but it seems not working, below my code: ColorDrawable[] color = {new ColorDrawable(ContextCompat.getColor(applicationContext, R.color.selectedcolor)), new ColorDrawable(ContextCompat.getColor(applicationContext, R.color.unselectedcolor))}; TransitionDrawable trans = new TransitionDrawable(color); holder.relativeLayout.setBackground(trans); trans.startTransition(5000); Colors are: #6400B7C5 #00000000 – Abduaziz Kayumov Nov 09 '17 at 15:03
15

It seems to be an old issue. I stumbled on this question while having a similar problem.

At the end it was just a bug in Android. The code is supposed to work, but the AnimatorInflater just blunders when setting the evaluator.

So setting the TypeEvaluator after the inflation again would do the trick.

    ObjectAnimator objAnim = (ObjectAnimator)AnimatorInflater.loadAnimator(getActivity(), R.animator.animator_bkg);
    objAnim.setTarget(view);
    objAnim.setEvaluator(new ArgbEvaluator());
    objAnim.start();

Set to new ArgbEvaluator() the animation works like intended.

tynn
  • 38,113
  • 8
  • 108
  • 143
  • 1
    This answer should be flagged as the actual answer because it solves the original question while the user1415536's answer uses an alternative approach with older objects. – ema3272 Mar 03 '18 at 07:47
  • I can't find the path R.animator.animator_bkg. Do I have to make that path myself or should I download something? – Daniëlle Feb 25 '21 at 22:38
  • 2
    @Daniëlle `animator_bkg` is the animator used by the author. So you'll need to create your own XML resource and use it instead. – tynn Feb 26 '21 at 07:57