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

I understand that the methods for pausing and resuming objectanimator objects are available only for the API: 19. However, since neither I nor half of Android users have this API, is there an alternative to make your animation stop and then resume it from the same state, not start from the beginning? Any help would be greatly appreciated.

+6
source share
1 answer

In my project, I had to make a rotation animation (it would pause and resume from the same / end position), and I solved it by getting the current time of the animator (when the animation ends / when I pause) and after starting the animator I set "setCurrentPlayTime ( with finite time). " To get the current time, I use getCurrentPlayTime (); and to set the time I use 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); } } 
+12
source

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


All Articles