Pause/Resume animation in API less than 19 (Android)?
Asked Answered
T

1

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.

Tafia answered 7/6, 2014 at 2:14 Comment(3)
possible duplicate of How to resume and pause ObjectAnimator in Android for API Levels below 19?Swagerty
actually that question is a duplicate of this one..thanks for the link thoughTafia
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.Swagerty
H
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);
    }
}
Hulky answered 10/10, 2014 at 14:53 Comment(2)
Its works with ObjectAnimator but how we can pause/resume ObjectAnimatorSet ?Blackington
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?Natasha

© 2022 - 2024 — McMap. All rights reserved.