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