Auto start animation

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

This is the abbreviation of my code:

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

All my resources are saved locally, so I add them to 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 view.

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

Now, before I can call the start() function, the animation starts as soon as the ImageView loads.

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

Did this happen to anyone?

**

EDIT:

**

Thanks to Tom, I know that the reason for starting the animation is the change in visibility that happens with the ImageView , which actively happens after the AnimationDrawable assigned. The solution in my case is not trivial, since I have a difficult situation, but for others it can be easier.

EDIT 2:

I will return to setting drawable as a background according to the class description , and I will quote:

The easiest way to create frame animation is to define the animation in an XML file placed in the res / drawable / folder folder and set this as the background for the View object. Then call start () to start the animation.

+4
source share
3 answers

This is entirely possible because you assign it as a wallpaper using setBackgroundDrawable - when you assign a background selection to the super.setVisibility call chain in the view and that setVisible(..) can trigger the animation.

The solution is to use ImageDrawable?

This is how I found the culprit.

As to why this is so, it is probably a design choice that I am too clueless to explain, although I can understand why, - he notes the difference between being a background and being a picture - the first is conceptually longer life is possible. (But this is geuss).

If you want to use the background settings approach, try saving invisible ImageViews until you want to show the animation.

+1
source

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

  • The easiest way to stop your animation that I found is as follows:

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

and what is he.

This essentially stops the animation when drawing at index 0. However, this is a workaround, but all the necessary two simple lines are all you need.

+1
source

Unfortunately, none of the workarounds mentioned above fixes the problem. My solution was to use:

 myView.setTranslationX(10000f); // to hide myView.setTranslationX(0f); // to show 
+1
source

Source: https://habr.com/ru/post/1493432/


All Articles