Animate change of view background color on Android

前端 未结 16 1065
孤街浪徒
孤街浪徒 2020-11-22 13:23

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

16条回答
  •  佛祖请我去吃肉
    2020-11-22 14:02

    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:

    
    
        
        
        
    
    

    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.

提交回复
热议问题