How to resume and pause ObjectAnimator in Android for API Levels below 19?
Asked Answered
P

3

11

I am aware that API level 19 supports pause() and resume() on ObjectAnimators. But in my project at API level 14, I have an ObjectAnimator which is applied to an Image view to rotate it. I want to pause the animation provided by the ObjectAnimator on touch and resume it from the place where the image view was (before touch down).

So I attempted to save the current play time and cancel the object animator on my stopAnimation() function.

private void stopAnimation(){
        currentTime = mGlobeAnimator.getCurrentPlayTime();
        mGlobeAnimator.cancel();
    }

In the startAnimation() function, I recreate the animator, set its target to the image view, set the saved play time and start it.

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

This does not work. Would anybody help please with any pointers to pause and resume animation provided by animator for API level before 19?

Pullen answered 10/8, 2014 at 18:25 Comment(0)
P
21

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);
}
Pullen answered 11/8, 2014 at 2:51 Comment(2)
ts works with ObjectAnimator but how we can pause/resume ObjectAnimatorSet ?Hardship
This doesn't work with repeatCount = Animation.INFINITEEdette
M
1

What you are doing is that it will just restart your animation, instead you can create a custom class for animation with pause and resume method.

You need to first check if the device is 19 api and above if it is then use the native pauses and resume of the object animator else use the regular Animation designed from api 1, you can follow this thread for pause and resume below api 19.

Misgiving answered 10/8, 2014 at 18:50 Comment(1)
The Custom Pausable Animation applies to View Animations. And it works fine. But I also have a frame layout which has certain image buttons placed. I need to rotate this frame layout so that the image buttons move along with the frame layout. If I use a custom view animation, the clicks for the image buttons reside in their original location (this being the reason why ObjectAnimators are chosen). How to pause and resume ObjectAnimators in this case?Pullen
E
0

Its probably too late for me to answer, i just came across this problem and i solved it using your method. i just added two things to your approach 1. In the start of animation check if the ** mCurrentPlayTime** is greater than zero.if its >0 then setCurrentPlayTime. otherwise its useless. 2. when your animation ends make it zero again.

Eastwood answered 13/1, 2016 at 9:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.