动画多个 TextView 的背景颜色

animate background color of multiple TextViews

我的 Activity 中有一些 TextViews,其中包含一些文本,我想同时为所有文本视图设置动画 background color

动画应该是 fade 从当前文本视图背景颜色到另一种颜色,例如红色,并从红色淡出到默认文本视图颜色。

可以做到吗?

一个解决方案可能是使用 TransactionDrawable

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/start_drawable" />
    <item android:drawable="@drawable/end_drawable" />
</transition>

其中 start_drawableend_drawable 是形状:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <solid android:color="@color/start_color"/>
</shape>

在运行时,

TransitionDrawable transition = (TransitionDrawable) textView.getBackground();
transition.startTransition(duration);

有了ObjectAnimator,感谢@pskink 指出,

final ObjectAnimator animaor = ObjectAnimator.ofObject(textView, "backgroundColor", new ArgbEvaluator(), startColor, endColor);
animaor.setDuration(300);
animaor.setRepeatCount(2);
animaor.setRepeatMode(ValueAnimator.REVERSE);
animaor.start();

ObjectAnimatorArgbEvaluator 需要 api 级别 11