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