0

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);
Zusee Weekin
  • 1,348
  • 2
  • 18
  • 41
athulya k
  • 145
  • 2
  • 10
  • Consult [this](http://stackoverflow.com/questions/10006736/pause-and-resume-translate-animation).It helped me.Hope it helps you. – kgandroid Apr 02 '14 at 05:37

2 Answers2

1

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.

Sree
  • 3,136
  • 2
  • 31
  • 39
  • i didn't understand how i can put that code in my existing code. can you please help me for that. I am a beginner to this, so please help me for that. – athulya k Apr 04 '14 at 08:57
  • subclass the animation type you'd like to use as i did , i dot know how to help you more, please goolge for more help – Sree Apr 04 '14 at 09:02
0

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
  • 204
  • 2
  • 8