2012-06-26 21 views
7

Quiero rotar la vista de la imagen con bucle continuo en android.mi código rotar funciona a la perfección sin establecer el modo de repetición. Si configuro el modo de repetición la animación de repetición no funciona, la vista de la imagen gira de forma estática y quiero animación de rotación de bucle, cualquiera me puede ayudar con mucho aprecio!Imagen Girar animación Looping no funciona en android

aquí está la animación xml

<?xml version="1.0" encoding="utf-8"?> 
<rotate 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromDegrees="45" 
    android:toDegrees="45" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:duration="100" 
    android:startOffset="0" 
/> 

aquí está mi clase java

import android.app.Activity; 
import android.os.Bundle; 
import android.view.animation.Animation; 
import android.view.animation.Animation.AnimationListener; 
import android.view.animation.AnimationUtils; 
import android.widget.ImageView; 

public class AnimationActivity extends Activity { 
    /** Called when the activity is first created. */ 
    ImageView my_image; 
    AnimationListener listener; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     listener = new AnimationListener() { 
      @Override public void onAnimationStart(Animation animation) {} 
      @Override public void onAnimationRepeat(Animation animation) {} 
      @Override 
      public void onAnimationEnd(Animation animation) { 
       System.out.println("End Animation!"); 
       //load_animations(); 
      } 
     }; 

     my_image=(ImageView)findViewById(R.id.my_img); 
     load_animations(); 



    } 
    void load_animations() 
    { 
     new AnimationUtils(); 
     Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation); 
     rotation.setRepeatCount(-1); 
     rotation.setRepeatMode(2); 
     rotation.setAnimationListener(listener); 
     my_image.startAnimation(rotation); 
    } 

} 

Gracias de antemano!

Respuesta

10

último, tengo la solución a tratar a continuación xml:

<?xml version="1.0" encoding="utf-8"?> 
<rotate 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromDegrees="0" 
    android:toDegrees="360" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:duration="2000" 
    android:repeatMode="reverse" 
    android:repeatCount="infinite" 
    android:startOffset="0" 
/> 

aquí está mi clase

import android.app.Activity; 
import android.os.Bundle; 
import android.view.animation.Animation; 
import android.view.animation.Animation.AnimationListener; 
import android.view.animation.AnimationUtils; 
import android.widget.ImageView; 

public class AnimationActivity extends Activity { 
    /** Called when the activity is first created. */ 
    ImageView my_image; 
    AnimationListener listener; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     listener = new AnimationListener() { 
      @Override public void onAnimationStart(Animation animation) {} 
      @Override public void onAnimationRepeat(Animation animation) {} 
      @Override 
      public void onAnimationEnd(Animation animation) { 
       System.out.println("End Animation!"); 
       //load_animations(); 
      } 
     }; 

     my_image=(ImageView)findViewById(R.id.my_img); 
     load_animations(); 



    } 
    void load_animations() 
    { 
     new AnimationUtils(); 
     Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation); 
     rotation.setAnimationListener(listener); 
     my_image.startAnimation(rotation); 
    } 

} 

Ese es el código está trabajando perfectamente!

Mi problema fue resuelto!

0

Después de investigar a través de las respuestas de internet, encontré una solución que funciona perfectamente para mí. (Y sí, el repeatCount y repeatMode es extremadamente defectuoso cuando se usa junto con animationSet).

anim_rotate_fade.xml:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
    android:ordering="together" > 

    <objectAnimator 
     android:duration="3000" 
     android:propertyName="rotation" 
     android:repeatCount="1" 
     android:valueTo="360" 
     android:valueType="floatType" /> 

    <objectAnimator 
     android:duration="3000" 
     android:propertyName="alpha" 
     android:repeatCount="1" 
     android:repeatMode="reverse" 
     android:valueFrom="0.0" 
     android:valueTo="0.3" 
     android:valueType="floatType" /> 

    <objectAnimator 
     android:duration="3000" 
     android:propertyName="y" 
     android:repeatCount="1" 
     android:repeatMode="reverse" 
     android:valueFrom="380" 
     android:valueTo="430" 
     android:valueType="floatType" /> 

</set> 

En actividad: (resolverlo mediante la introducción de un ligero retraso después de la animación terminó).

ImageView starlightImageView = new ImageView(this); 
starlightImageView.setImageResource(R.drawable.starlight); 
final AnimatorSet animate = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.anim.anim_rotate_fade); 
AnimatorListenerAdapter animatorListener = new AnimatorListenerAdapter() { 
    @Override 
    public void onAnimationEnd(Animator animation) { 
     super.onAnimationEnd(animation); 
     new Handler().postDelayed(new Runnable() { 
      @Override public void run() { 
       animate.start(); 
      } 
     }, 1000); 
    } 
}; 
animate.setTarget(starlightImageView); 
animate.addListener(animatorListener); 

Hay una gran cantidad de clases que le gustaría a la investigación, pero actualmente estoy usando objectAnimator que es altamente flexible. Yo no recomendaría el uso de Animación o AnimationUtils:

  • Animación
  • AnimationUtils
  • animador
  • AnimatorInflater
  • AnimatorListener
  • AnimatorListenerAdapter