问题描述
你如何动画在Android的视图的背景颜色的变化?
How do you animate the change of background color of a view in Android?
例如:
我有一个红色的背景颜色的看法。视图的背景颜色变为蓝色。我该怎么办颜色之间的平滑过渡?
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.
推荐答案
我最终找出一个(pretty的好)解决方案,这个问题!
I ended up figuring out a (pretty good) solution for this problem!
您可以使用TransitionDrawable要做到这一点。例如,在绘制文件夹中的XML文件,你可以写这样的:
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>
然后,在你的XML的实际查看,你会引用此TransitionDrawable在机器人:背景
属性
在这一点上,你可以通过执行启动转型的指令你的code:
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);
我希望这可以帮助你解决问题!
I hope this helps you solve your problem!
编辑:查看罗马的回答使用属性动画API的另一种解决方案,其中WASN 'T提供的这个答案最初发布的时间。
See Roman's answer for another solution using the Property Animation API, which wasn't available at the time this answer was originally posted.
这篇关于动画视图背景颜色的Android变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!