androidanimation

How to start Android animation from the values where it was stopped?


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


Solution

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