/ / animación no se aplica a un botón con fondo - android, animación

La animación no se aplica a un botón con fondo - Android, animación

Tengo un botón definido en mi archivo de diseño. Cuando se hace clic en el botón, le asigno una animación de la siguiente manera:

   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);

todas las cosas funcionan bien hasta este punto Pero el problema surge cuando configuro cualquier fondo para mi botón especialmente android:background="@null" en la definición del botón.

Alguna idea de como arreglar esto ? Gracias.

Respuestas

0 para la respuesta № 1

Acabo de probar esto en mi proyecto y funciona en un botón con fondo.

private ObjectAnimator rotationAnimator;

y las funciones para controlar la animación.

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();
}