java - 如何在 AppCompatButton 旋转时更改其背景?

标签 java android xml animation transformation

我正在使用 RotateAnimation 来旋转 AppCompatButton(用作工具栏按钮),并且我想在旋转时更改其背景。

到目前为止我没有找到任何有用的主题。

这是我的代码:

AppCompatButton mBtn = (AppCompatButton) view.findViewById(R.id.search_btn);
        Animation mRotateAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotation);
mBtn.setAnimation(mRotateAnimation);
mBtn.startAnimation(mRotateAnimation);

旋转.xml:

<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="0"
    android:duration="600" />

我想在动画的开始和结束之间获得平滑的背景变化。任何有用的资源链接或代码都会很棒。谢谢大家!

最佳答案

您应该向您的 View 添加另一个 ValueAnimation

AppCompatButton mBtn = (AppCompatButton) view.findViewById(R.id.search_btn);
        Animation mRotateAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotation);
mBtn.setAnimation(mRotateAnimation);
mBtn.startAnimation(mRotateAnimation);

int colorFrom = getResources().getColor(R.color.red);
int colorTo = getResources().getColor(R.color.blue);
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.setDuration(600);
colorAnimation.addUpdateListener(new AnimatorUpdateListener() {

    @Override
    public void onAnimationUpdate(ValueAnimator animator) {
        mBtn.setBackgroundColor((int) animator.getAnimatedValue());
    }

});
colorAnimation.start();

关于java - 如何在 AppCompatButton 旋转时更改其背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57320622/

相关文章:

java - Logback 的 SiftingAppender 的后备附加程序

java - getWindow().getAttributes() 期间出现 NullpointerException;

android - 使用应用程序操作发布 Android 应用程序时出错

C# XElement : Node Formatting with HTML

java - 如何锁定写入文件以便其他消费者无法使用

java - 如何使用java为亚马逊AWS的存储桶中的单个文件设置权限

java - 如何反转单链表 - Java

android - 如何以编程方式格式化 SD 卡上的所有数据?

php - 使用 PHP 从 XML 中删除空标签

java - 如何在BeanIO中设置字符编码?