How to resume and pause ObjectAnimator in Android for API levels below 19?

I know that API level 19 supports pause () and resume () for ObjectAnimators. But in my project at API level 14, I have an ObjectAnimator that is used to view an image to rotate it. I want to pause the animation provided by ObjectAnimator with a touch and resume it from where the image was (until it touches).

So, I tried to save the current playback 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 playback 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. Will anyone help with any pause pointers and resume animations provided by the animator for API level up to 19?

+6
source share
3 answers

I think I started working by running the animator and then setting currentPlayTime (). The documentation clearly states (that I just came across) that if the animation was not running, the currentPlayTime set using this method will not advance!

Sets the position of the animation at a specified point in time. This time should be from 0 to the total duration of the animation, including any repetition. If the animation is not already running, it will not move forward after it is installed this time; he will simply set the time for this value and take any appropriate action based on that time. If the animation is already running, setCurrentPlayTime () will set the current playback 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); } 
+10
source

What you do is that it will simply restart your animation, instead you can create your own class for the animation using the pause and resume method.

You need to first check if the device is 19 api and above, if it then uses its own suspensions and resume animating the object, otherwise use the usual animation created from api 1, you can follow this to pause and resume below api 19.

+1
source

It may be too late for me to answer, I just ran into this problem and I decided to use your method for it. I just added two things to your approach 1. At the beginning of the animation, check that ** mCurrentPlayTime ** is greater than zero. If it is> 0, then set the value to CurrentPlayTime. otherwise it is useless. 2. When your animation ends, it will again become zero.

0
source

Source: https://habr.com/ru/post/970493/


All Articles