gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:23:15 29 4
gpt4 key购买 nike

我正在使用 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com