Yann Masoch
Yann Masoch

Reputation: 1630

AnimationDrawable start at specific frame?

I have the same question as this question asked 2 years ago with no valid answer (please do not close this question for duplicated content; there is no answer/solution yet).

So, I have a AnimationDrawable made programmatically and I would like to start the animation at a specific frame.

When I stop and I start the animation again by specifying a frame the animation displays the right frame but just after jumps back to the 1st frame to run the animation. For example:

// Stopping the animation 
animation.stop();

// Starting the animation again
animation.start();
animation.selectDrawable(9);

What should be the right practice?

Do I have to recreate the animation on the fly and set the frame 9 as 1st frame in the animation such as animation.addFrame(myDrawable9, duration) and the next frames would be 10, 11, 12...1, 2, 3, 4, 5, 6, 7, 8?

Any fast an easy way to do that?

Thx!

Upvotes: 2

Views: 1418

Answers (1)

Kevin Coppock
Kevin Coppock

Reputation: 134664

So, unfortunately there's no public setFrame() method. However, it implements Runnable, and the implementation of run() calls nextFrame(false) which calls through to setFrame(). So, it's certainly a hack, but you could implement a method such as:

public static void setAnimationFrame(AnimationDrawable anim, int frame) {
    for (int i = 0; i < frame; i++) {
        anim.run();
    }
}

Which should advance the Drawable to the proper frame.

Upvotes: 1

Related Questions