Image Rotation Animation Loop does not work on Android

I want to rotate a continuous loop image on android.my. Turning codes works fine with snooze disabled. If I set the re-re-animation of the replay, it doesn't work, but viewing the image statically rotates some angle, and I want the loop to rotate the animation, anyone can help me with a big estimate!

here is the xml animation

<?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" /> 

here is my java class

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

Thank Advance

+7
source share
2 answers

Finally, I got the solution to try under 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" /> 

here is my class

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

That code works fine!

My problem is solved!

+10
source

After exploring the answers from the Internet, I found solutions that work great for me. (And yes, repeatCount and repeatMode are extremely distorted when used with 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> 

In action: (Solve it by introducing a slight delay after the animation ends).

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

There are many classes you would like to explore, but I am currently using objectAnimator, which is very flexible. I would not recommend using Animation or AnimationUtils:

  • Animation
  • AnimationUtils
  • Animator
  • AnimatorInflater
  • Animatorlistener
  • AnimatorListenerAdapter
0
source

All Articles