J'ai un bouton défini dans mon fichier de mise en page. Lorsque le bouton est cliqué, je lui attribue une animation comme suit:
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);
toutes les choses fonctionnent bien à ce point. Mais le problème est soulevé lorsque je fixe un fond à mon bouton spécialement android:background="@null"
dans la définition du bouton.
Une idée pour réparer ceci ? Merci.
Réponses:
0 pour la réponse № 1Je viens d’essayer cela dans mon projet et cela fonctionne sur un bouton avec un fond.
private ObjectAnimator rotationAnimator;
et les fonctions pour contrôler l'animation
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();
}