How do I pause frame animation using AnimationDrawable? [closed]
Asked Answered
R

2

8

How do I pause frame animation using AnimationDrawable?

Ripleigh answered 19/5, 2010 at 10:4 Comment(0)
R
25

I realize this thread is quite old, but since this was the first answer on Google when I was searching for a way to pause an animation, I'll just post the solution here for someone else to see. 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.

Richela answered 20/10, 2011 at 19:4 Comment(3)
Thanks for share with us this is very helpful +1 from my side!Abnormity
@Abnormity it can be work for frame animationPaulettapaulette
@Richela it can be work for frame animationPaulettapaulette
P
2

From the API:

Animations do not have a pause method.

http://www.androidjavadoc.com/1.0_r1/android/view/animation/package-summary.html

Proxy answered 19/5, 2010 at 10:6 Comment(1)
thanks for the reply. Yes i am aware that there is no pause method, instead i implemented a custom class which implements Runnable and used postDelayed, removeCallbacks methods to accomplish the task. I am not sure whether this is a proper way of doing it.Ripleigh

© 2022 - 2024 — McMap. All rights reserved.