5

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

elizabetht
  • 627
  • 1
  • 8
  • 16
  • post the code where you used the animation xml – Rod_Algonquin Aug 06 '14 at 18:26
  • @Rod_Algonquin: Updated the code where I set the view with the animation – elizabetht Aug 06 '14 at 18:29
  • also post stopAnimationForImageButton method – Rod_Algonquin Aug 06 '14 at 18:30
  • stopAnimationForImageButton() nothing but to stop the object animator for the image buttons I have on the layout which should also move along with the image view. `private void stopAnimationForImageButton() { for(ObjectAnimator objectAnimator : mObjectAnimatorList) { objectAnimator.cancel(); objectAnimator.end(); } mObjectAnimatorList.clear(); } ` – elizabetht Aug 06 '14 at 18:39
  • @Rod_Algonquin: I have posted the relevant code. Could you pls help with any pointers? or what you see as wrong here? – elizabetht Aug 06 '14 at 18:49

2 Answers2

4

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);
    }
elizabetht
  • 627
  • 1
  • 8
  • 16
0

The pause feature has been added in API level 19. Here you can read how implement it. This method use ObjectAnimator which is not much more complicated than usual Animation class that you use. However, there is another alternative trick that can be useful.
With best regards.

Community
  • 1
  • 1
Roman
  • 166
  • 1
  • 7
  • I now changed to use ObjectAnimator because of its simplicity. But I am at API level 14 and hence cannot use pause() or resume(). Is there an alternative to use pause() and resume() in ObjectAnimator in API level 14? Pls help – elizabetht Aug 10 '14 at 16:55
  • It's interesting question. Actually, answer is no. But you can try use **cancel()** method for pausing and when you want to resume just re-create/clone *ObjectAnimator* with **clone()** method and manually set the current position of animation in time to this ObjectAnimator instance. For this use `ObjectAnimator.getCurrentPlayTime()` before pausing. I haven't tried this method but it seems to be working. – Roman Aug 10 '14 at 17:48
  • Do you mean to say that I do the following? `private long currentTime; private ObjectAnimator globeAnimator; private void stopAnimation() { currentTime = globeAnimator.getCurrentPlayTime(); globeAnimator.cancel(); } private void startAnimation() { ObjectAnimator globeAnimatorClone = globeAnimator.clone(); globeAnimatorClone.setTarget(globeImageView); globeAnimatorClone.setCurrentPlayTime(currentTime); globeAnimatorClone.start(); }` – elizabetht Aug 10 '14 at 17:59
  • No, it doesn't seem to work. Is there something wrong? – elizabetht Aug 10 '14 at 18:10
  • try to **clone()** ObjectAnimator before **cancel()**. Or even better not to clone ObjectAnimator and just create new *ObjectAnimator* initializing it as your first *ObjectAnimator* with **setCurrentPlayTime(currentTime)** – Roman Aug 10 '14 at 18:19
  • I tried that too. It did not work! :( `private void startAnimation(Context context, View view, float startAngle) {ObjectAnimator globeAnimatorClone = (ObjectAnimator)AnimatorInflater.loadAnimator(context, R.animator.rotate_globe); globeAnimatorClone.setTarget(mImageView); globeAnimatorClone.setCurrentPlayTime(currentTime); globeAnimatorClone.start(); }` – elizabetht Aug 10 '14 at 18:27
  • hmm.. If you find solution let me know. – Roman Aug 10 '14 at 18:49
  • I have updated my answer. Pls let me know if you see anything wrong! – elizabetht Aug 11 '14 at 02:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59079/discussion-between-elizabetht-and-roman). – elizabetht Aug 11 '14 at 04:24