How to stop the animation at the end of the loop?

I have an ImageView that I rotate to use as a loading animation. Once my data is uploaded, I try to stop the animation, but instead of moving to the end and then stopping, the animation goes halfway and then stops, and then the image returns to its original state, which looks pretty ugly.

Here is what I tried:

Option 1:

ImageView iv = (ImageView) findViewById(R.id.refreshImage); if (iv != null) { iv.clearAnimation(); } 

Option 2:

 ImageView iv = (ImageView) findViewById(R.id.refreshImage); if (iv != null && iv.getAnimation() != null) { iv.getAnimation().cancel(); } 

Option 3:

 ImageView iv = (ImageView) findViewById(R.id.refreshImage); if (iv != null && iv.getAnimation() != null) { iv.getAnimation().setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { animation.cancel(); } @Override public void onAnimationEnd(Animation animation) { } }); } 

The end result is the same in all three cases. How to rotate the image and return it to where it was launched?

Edit:

Additional Information: My rotation animation:

 <?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fromDegrees="0" android:interpolator="@android:anim/linear_interpolator" android:pivotX="50%" android:pivotY="50%" android:toDegrees="360" /> 

How do I run the animation:

 ImageView iv = (ImageView) findViewById(R.id.refreshImage); Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotate); rotation.setRepeatCount(Animation.INFINITE); iv.startAnimation(rotation); 
+6
source share
3 answers

Repeat the animation ad infinitum until it starts, and then, when the action finishes, set the animation to repeat the counter to 0. The animation will end the current loop and stop without the transition you want to avoid.

 //How you start Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotate); rotation.setRepeatCount(Animation.INFINITE); iv.startAnimation(rotation); //You do your stuff while it spins ... //You tell it not to repeat again rotation.setRepeatCount(0); 

It is important that you first set it to Animation.INFINITE (or -1, as they did the same), and then to 0 if you set it to 1000 , for example, then it will not stop for any reason in compliance with my tests.

+14
source
0
source

iv.clearAnimation (); to stop the animation

-2
source

Source: https://habr.com/ru/post/948087/


All Articles