miroku
miroku

Reputation: 176

How to set a specific frame in android animation

I have a frame-by-frame animation and I want to set a specific frame when the stop method has been called. I searched a lot but I haven't found what I was serching for.

Does this method exists? Is it possible to do that?

Upvotes: 3

Views: 3232

Answers (2)

muyiou
muyiou

Reputation: 669

Use this method on your AnimationDrawable: animationDrawable.selectDrawable(index)

Upvotes: 4

jobesu
jobesu

Reputation: 620

Assuming that frame-by-frame means AnimationDrawable given in the background of an ImageView. Like that (or via xml):

        AnimationDrawable animationDrawable = new AnimationDrawable();
        animationDrawable.addFrame(drawableFrame1, 0);
        animationDrawable.addFrame(drawableFrame2, 1);
        animationDrawable.addFrame(drawableFrame3, 2);
        animationDrawable.addFrame(drawableFrame4, 3);
        iv = new ImageView(this);
        iv.setBackgroundDrawable(animationDrawable);

Here is the code to retrieve a given frame and set it to the background of the ImageView

      onStop() {
            Drawable drawableFrame2 = ((AnimationDrawable)iv.getBackground()).getFrame(2);
            iv.setBackgroundDrawable(drawableFrame2);
            iv.postInvalidate();
      }

Upvotes: 1

Related Questions