悬浮操作按钮背景色渐变动画

4

我想要对FloatingActionButton的backgroundTint值(最好还有alpha值)进行动画处理,使FAB背景颜色不断地在两种颜色之间切换。

我的方法是使用定时器来调用一个更新该属性的函数。但我相信应该有更好的方法做到这一点?


你的意思是想定期更改(设置)FAB的背景颜色,还是实际上连续动画化它(基本上是将一个颜色值淡入另一个颜色值)?如果是后者,我肯定会使用ObjectAnimator或ValueAnimator。一些实现细节的基本想法在Cyril Mottier的这篇G+帖子中概述了。 - MH.
我认为处理这个问题的理想方式是动画。 - Max
@MH。我理想中想要实现交叉淡入淡出效果。感谢您的建议。 - user1889776
1个回答

14

我按照@MH.的建议使用ObjectAnimator使其工作,但我必须重写onAnimationUpdate()回调函数:

final ValueAnimator animator = ValueAnimator.ofInt(Color.rgb(0, 121, 107), Color.rgb(226, 143, 34));
animator.setDuration(2000L);
animator.setEvaluator(new ArgbEvaluator());
animator.setInterpolator(new DecelerateInterpolator(2));
animator.addUpdateListener(new ObjectAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        int animatedValue = (int) animation.getAnimatedValue();
        fab.setBackgroundTintList(ColorStateList.valueOf(animatedValue));
    }
});
animator.start();

假设我有一个百分比表示还有多少要完成,而不是使用 animation.getAnimatedValue ,我该如何使用它呢? - android developer
使用ValueAnimator.ofObject,并使用ArgbEvaluator代替:ValueAnimator.ofObject(ArgbEvaluatorCompat.getInstance(), prevColor, newColor).start(); - BamsBamx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接