Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 800
Bazinga Avatar asked Jun 07 '14 02:06

Bazinga


1 Answers

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);
    }
}
like image 126
nikolaDev Avatar answered Oct 02 '22 07:10

nikolaDev