How can I pause and restart animations in Android?

How can i pause and restart an animations in Android?

  • Can anyone please tell me how to pause and restart an animations in Android. Animation XML is: <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:fromXDelta="500%" android:toXDelta="0%" android:fromYDelta="0%" android:toYDelta="0%" android:duration="10000" android:fillAfter="true" android:repeatCount="infinite"/> </set> And the code is: Animation am=AnimationUtils.loadAnimation(this, R.anim.note); music1.startAnimation(am);

  • Answer:

    There's no supporting API for pausing/resuming animations in API level 11. The features have been added in API level 19. What you need to do is subclass the animation type you'd like to use and then add methods for pausing and resuming the animation. Here is an example for AlphaAnimation: public class PausableAlphaAnimation extends AlphaAnimation { private long mElapsedAtPause=0; private boolean mPaused=false; public PausableAlphaAnimation(float fromAlpha, float toAlpha) { super(fromAlpha, toAlpha); } @Override public boolean getTransformation(long currentTime, Transformation outTransformation) { if(mPaused && mElapsedAtPause==0) { mElapsedAtPause=currentTime-getStartTime(); } if(mPaused) setStartTime(currentTime-mElapsedAtPause); return super.getTransformation(currentTime, outTransformation); } public void pause() { mElapsedAtPause=0; mPaused=true; } public void resume() { mPaused=false; } } This will keep increasing your starttime while the animation is paused, effectively keeping it from finishing and keeping it's state where it was when you paused. Hope it helps someone.

athulya k at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

and after start your animation you have only call pause and resume methods. public class TranslateAnim extends TranslateAnimation{ public TranslateAnim(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) { super(fromXDelta, toXDelta, fromYDelta, toYDelta); // TODO Auto-generated constructor stub } private long mElapsedAtPause=0; private boolean mPaused=false; @Override public boolean getTransformation(long currentTime, Transformation outTransformation) { if(mPaused && mElapsedAtPause==0) { mElapsedAtPause=currentTime-getStartTime(); } if(mPaused) setStartTime(currentTime-mElapsedAtPause); return super.getTransformation(currentTime, outTransformation); } public void pause() { mElapsedAtPause=0; mPaused=true; } public void resume() { mPaused=false; }

Shivansh Saxena

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.