Android objectAnimator 动画背景颜色的布局

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11097693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:59:46  来源:igfitidea点击:

Android objectAnimator animate backgroundColor of Layout

androidandroid-animation

提问by XMight

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

我有个问题。我想为a的背景颜色设置动画LinearLayout,使用ObjectAnimator.
问题是它有动画,但它既不关心持续时间也不关心valueFromvalueTo

This is my xml file:

这是我的 xml 文件:

<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:

在 Java 中,我这样称呼:

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?

请注意,当我为布局的 alpha 设置动画时,它会按预期工作。
这是 Android 错误(华硕 Transformer 上的 4.0.3),还是我错过了什么?

回答by user1415536

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

我用谷歌搜索了一下。有一个答案。尝试使用 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.

此外,stackoverflow.com 上某处有一个主题专门针对同一问题。

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);

回答by tynn

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.

最后它只是Android中的一个错误。代码应该可以工作,但是 AnimatorInflater 在设置评估器时出错了。

So setting the TypeEvaluatorafter the inflation again would do the trick.

因此,TypeEvaluator再次设置通货膨胀之后就可以了。

    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.

设置为new ArgbEvaluator()动画效果如预期。