AnimationDrawable not playing

2019-02-05 19:06发布

问题:

So I want my animation to start as soon as the activity is created, but for some reason no matter what I try will get it to start. I can get it to start by having a click event but I want it to start all on its own.

Here's what I have and how do I get this to work?

package tween.learn;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;

public class Animate extends Activity {

    public ImageView image;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        ImageView tweenImage = (ImageView) findViewById(R.id.imageView1);
        tweenImage.setBackgroundResource(R.anim.cubicfacetween);

        AnimationDrawable frameAnimation = 
                           (AnimationDrawable) tweenImage.getBackground();
        frameAnimation.start();

        }



}

Thanks

回答1:

I think you have to start the animation after initialization of the view in question is complete. You should be able to do something like:

final ImageView tweenImage = (ImageView) findViewById(R.id.imageView1);
tweenImage.setBackgroundResource(R.anim.cubicfacetween);      
tweenImage.post(new Runnable() {
    @Override
    public void run() {
        AnimationDrawable frameAnimation =
            (AnimationDrawable) tweenImage.getBackground();
        frameAnimation.start();
    }
}

Edit - this question led me to believe that the onWindowFocusChanged method won't always work. It does seem simpler and is probably a better idea if it works for you.



回答2:

I know this is an old question, but I was having the same problem, but the answers weren't helping me. After spending hours working on it I finally found out that my problem was that I had added android:src="@drawable/myanimation" to the imageview container. Once I removed this the above answers worked. I think the animation was running but by setting the src the first image of the animation was on top of it, and thus I thought it wasn't playing.

  • My final code has an XML file with the animation saved in the drawable folder
  • My layout has an imageview with no android:src defined and is set to invisible
  • In onCreate I set the imageview to visible and setBackgroundResource to my animation described in the XML file
  • in onWindowFocusChanged I start the animation.


回答3:

Try starting your animation after the window gets focus by overriding onWindowFocusChanged in your Activity:

  @Override
  public void onWindowFocusChanged (boolean hasFocus)
  {
      //Start animation here
  }

See docs here: http://developer.android.com/reference/android/app/Activity.html#onWindowFocusChanged%28boolean%29



回答4:

My solution for playing animations while they are visible. It let you declare (in xml layout) and forget.

class AnimatedImageView extends ImageView {
    // required constructors omitted for clarity 

    private void updateAnimationsState() {
        boolean running = getVisibility() == View.VISIBLE && hasWindowFocus();
        updateAnimationState(getDrawable(), running);
        updateAnimationState(getBackground(), running);
    }

    private void updateAnimationState(Drawable drawable, boolean running) {
        if(drawable instanceof AnimationDrawable) {
            AnimationDrawable animationDrawable = (AnimationDrawable) drawable;
            if(running) {
                animationDrawable.start();
            } else {
                animationDrawable.stop();
            }
        }
    }

    @Override
    protected void onVisibilityChanged(View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        updateAnimationsState();
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        updateAnimationsState();
    }
}

If you setup drawable from code, then you should override appropriate set*() methods and call updateAnimationsState() from there.



回答5:

One more solution:

Call animationDrawable.stop() before calling start() method.



回答6:

I fixed this problem by call setBackgroundResource instead of setImageResource when i want to start the animation as mentioned in the above answers

private AnimationDrawable animationDrawable;


 fab.setImageResource(R.drawable.animation);
 animationDrawable = (AnimationDrawable) fab.getDrawable();
 fab.setOnClickListener(new Button.OnClickListener() {
    @Override
    public void onClick(View v) {
        fab.setBackgroundResource(R.drawable.animation);
        animationDrawable.start();
    }
 });

note: to reset the animation use

animationDrawable.selectDrawable(0);//animation drawable used here is collection of images
animationDrawable.stop();