Mam przycisk zdefiniowany w moim pliku układu. Po kliknięciu przycisku przypisuję mu animację w następujący sposób:
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);
do tej pory wszystko działa dobrze. Ale problem pojawia się, gdy specjalnie ustawiam dowolne tło mojego przycisku android:background="@null"
w definicji przycisku.
Masz pomysł, jak to naprawić? Dzięki.
Odpowiedzi:
0 dla odpowiedzi № 1Właśnie próbowałem tego w moim projekcie i działa on na przycisku z tłem.
private ObjectAnimator rotationAnimator;
oraz funkcje kontrolujące animację
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();
}