12

I realize that the pause and resume methods on objectanimator objects are only available for API:19. However, since neither I, nor half of the android users out there have this API, is there an alternative to get your animation to pause and then resume from the same state instead of starting back from the beginning? Any help would be greatly appreciated.

Bazinga
  • 489
  • 1
  • 5
  • 16
  • possible duplicate of [How to resume and pause ObjectAnimator in Android for API Levels below 19?](http://stackoverflow.com/questions/25231707/how-to-resume-and-pause-objectanimator-in-android-for-api-levels-below-19) – Alex Cohn Mar 31 '15 at 11:10
  • actually that question is a duplicate of this one..thanks for the link though – Bazinga Apr 05 '15 at 20:24
  • There is no negative connotation in marking a question "duplicate", at least as far as I understand. On the other hand, SO encourages us to choose a duplicate by *answers*, therefore my choice was to place the marker for the one that was answered earlier. – Alex Cohn Apr 07 '15 at 08:05

1 Answers1

21

In my project I had to make rotate animation (that will be pause and than resume from the same/end position), and I solved it by getting the current time of the animator (when animation ends/when I click pause) and than, after starting the animator I'm setting the "setCurrentPlayTime(with the ending time)". For getting the current time I'm using getCurrentPlayTime(); and for setting the time I'm using setCurrentPlayTime() of the ObjectAnimator class.

References: http://developer.android.com/reference/android/animation/ValueAnimator.html#setCurrentPlayTime(long) http://developer.android.com/reference/android/animation/ValueAnimator.html#getCurrentPlayTime()

private ObjectAnimator mObjectAnimator;
private long mAnimationTime;

private void stopAnimation() {
    if(mObjectAnimator != null) {
        mAnimationTime = mObjectAnimator.getCurrentPlayTime();
        mObjectAnimator.cancel();
    }
}

private void playAnimation() {
    if (mObjectAnimator != null) {
        mObjectAnimator.start();
        mObjectAnimator.setCurrentPlayTime(mAnimationTime);
    }
}
nikolaDev
  • 1,802
  • 2
  • 14
  • 15
  • Its works with ObjectAnimator but how we can pause/resume ObjectAnimatorSet ? – Deven May 31 '17 at 13:40
  • Hello NikolaDev, i tried implementing the solution you provided on cancel the animation pause, but when i start and then set the currentplaytime the animation restart do you have any idea what could be causing this? – Z. Kiwan Jun 19 '17 at 23:39