Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AnimationDrawable auto starts

I have a problem with an AnimationDrawable that I create programatically that starts as soon as I assign it to an ImageView via imageView.setBackgroundDrawable (I support API 8).

This is an abbreviation of my code:

 mSequence = new AnimationDrawable();
 ImageView imageView = new ImageView(context);
 ImageView.setAdjustViewBounds(false);

All of my assets are saved locally so I add them to the AnimationDrawable

for(String assetId : mAssets) {
    bitmap = loadBitmap(assetId); // returns a bitmap saved earlier
    if (bitmap != null) {
        mSequence.addFrame(new BitmapDrawable(res, bitmap), mFrameDuration);
    }
}

And finally I assign the AnimationDrawable to the view

if (mSequence.getNumberOfFrames() > 0) {
    imageView.setBackgroundDrawable(mSequence);
}

Now before I have a chance to call the start() function the animation starts as soon as the ImageView is loaded.

I want to be able to control when the animation starts according to my own logic.

Has this happened to anyone?

**

EDIT:

**

Thanks to Tom, I know the reason for the triggering of the animation is the change in visibility that happens to the ImageView that actively happens after assigning the AnimationDrawable. The solution in my case isn't trivial since I have a complicated situation but for others it might be simpler.

EDIT 2:

I'm back to setting the drawable as the background according to class description, and I quote:

The simplest way to create a frame-by-frame animation is to define the animation in an XML file, placed in the res/drawable/ folder, and set it as the background to a View object. Then, call start() to run the animation.

like image 867
Itai Hanski Avatar asked Oct 21 '22 04:10

Itai Hanski


2 Answers

It's quite possibly because you're assigning it as a background drawable with setBackgroundDrawable- when you assign a background drawable a chain of calls to super.setVisibility ends up in View, and that setVisible(..) can trigger an animation.

The solution is to use ImageDrawable?

Here's how I found the culprit.

As for why it's like that, it's probably a design choice about which I am too ignorant to explain, allthough I can see why- it marks out the difference between being a background and being a picture- the former has a conceptually longer life-span, perhaps. (But that's a geuss).

If you want to use the setting backgrounds approach, perhaps try keeping your ImageViews invisible until you want to show the animation.

like image 102
Tom Avatar answered Oct 24 '22 04:10

Tom


I know this is more than a year old but I will share my experience.

  • The simplest way to stop your animation, that I have found, is to do this:

    mImageView.setImageDrawable(mAnimationDrawable);
    mAnimationDrawable.stop();
    mAnimationDrawable.selectDrawable(0);
    

and that's it.

This essentially stops the animation on the drawable at index 0. It is a workaround, however, but an easy two extra lines is all that is needed.

like image 31
Glo Avatar answered Oct 24 '22 04:10

Glo