我的布局文件中定义了一个按钮。单击该按钮时,我将为其分配动画,如下所示:
Animation animation = new RotateAnimation(0.0f, 360.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
animation.setRepeatCount(-1);
animation.setDuration(2000);
mybutton.setAnimation(animation);
到目前为止,一切正常。但是,当我在按钮定义中专门为我的按钮android:background="@null"
设置任何背景时会出现问题。
有关如何解决此问题的任何想法?谢谢。
答案 0 :(得分:0)
我刚刚在我的项目中试过这个,它适用于带背景的按钮。
private ObjectAnimator rotationAnimator;
以及控制动画的功能
private void startAnimation(int animationDuration) {
if (rotationAnimator == null || !rotationAnimator.isRunning()) {
// You can tweak this to your needs
Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
Keyframe kf2 = Keyframe.ofFloat(0.5f, 180f);
Keyframe kf1 = Keyframe.ofFloat(1f, 360f);
PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe("rotation", kf0, kf1, kf2);
rotationAnimator = ObjectAnimator.ofPropertyValuesHolder(your_button_here, pvhRotation);
rotationAnimator.setRepeatCount(ObjectAnimator.INFINITE);
rotationAnimator.setInterpolator(new LinearInterpolator());
rotationAnimator.setDuration(animationDuration);
rotationAnimator.start();
}
else {
Log.d("Animation", "I am already running!");
}
}
private void stopAnimation() {
if (rotationAnimator != null) {
rotationAnimator.cancel();
rotationAnimator = null;
}
}
private boolean getAnimationRunning() {
return rotationAnimator != null && rotationAnimator.isRunning();
}