I have an image view on which I apply a rotate animation. The animation works fine. On touch down, I attempt to stop the rotate animation using cancel(), resetting the animation using reset() and clearing the animation on the view using clearAnimation(). But the animation comes back to the original position. How to stop animation with the values it was when touch down event happens and restart from where it was stopped?
My animation is defined in the xml as below
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="360"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="true"
android:fillEnabled="true"
android:duration="200000"
android:repeatMode="restart"
android:repeatCount="infinite"
android:interpolator="@android:anim/linear_interpolator"/>
I am attempting to stop the animation using the following code
private void stopAnimation(){
mRotateAntiClockwiseAnimation.cancel();
mRotateAntiClockwiseAnimation.reset();
imageView.clearAnimation();
mRotateAntiClockwiseAnimator.end();
mRotateAntiClockwiseAnimator.cancel();
stopAnimationForImageButton();
}
I am setting the animation on my view using the following code
mRotateAntiClockwiseAnimation = AnimationUtils.loadAnimation(context, R.anim.rotate_anticlockwise);
mRotateAntiClockwiseAnimation.setFillEnabled(true);
mRotateAntiClockwiseAnimation.setFillAfter(true);
imageView.setAnimation(mRotateAntiClockwiseAnimation);
mRotateAntiClockwiseAnimation.startNow();
imageView.invalidate();
As u see, even using cancel() or reset() did not help to stop the animation at the point where it was touched down. Any pointers would help
You can find your animation file by navigating to the app > res > anim > zoomanimation.
Navigate to the app > res > Right-Click on res >> New >> Directory >> Name your directory as “anim”. Inside this directory, we will create our animations. For creating a new anim right click on the anim directory >> Animation Resource file and give the name to your file.
Android's transition framework allows you to animate all kinds of motion in your UI by simply providing the starting layout and the ending layout.
On Android 4.4 (API level 19) and higher, you can use the transition framework to create animations when you swap the layout within the current activity or fragment. All you need to do is specify the starting and ending layout, and what type of animation you want to use.
I think I got it working by starting the animator and then setting the currentPlayTime(). The documentation clearly tells (which I just stumbled upon) that if the animation has not been started, the currentPlayTime set using this method will not advance the forward!
Sets the position of the animation to the specified point in time. This time should be between 0 and the total duration of the animation, including any repetition. If the animation has not yet been started, then it will not advance forward after it is set to this time; it will simply set the time to this value and perform any appropriate actions based on that time. If the animation is already running, then setCurrentPlayTime() will set the current playing time to this value and continue playing from that point. http://developer.android.com/reference/android/animation/ValueAnimator.html#setCurrentPlayTime(long)
private void stopAnimation(){
mCurrentPlayTime = mRotateAntiClockwiseAnimator.getCurrentPlayTime();
mRotateAntiClockwiseAnimator.cancel();
}
private void startAnimation() {
mRotateAntiClockwiseAnimator.start();
mRotateAntiClockwiseAnimator.setCurrentPlayTime(mCurrentPlayTime);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With