AnimationDrawable not playing

前端 未结 6 742
独厮守ぢ
独厮守ぢ 2020-12-31 00:22

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 ev

相关标签:
6条回答
  • 2020-12-31 00:33

    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.

    0 讨论(0)
  • 2020-12-31 00:35

    One more solution:

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

    0 讨论(0)
  • 2020-12-31 00:36

    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.
    0 讨论(0)
  • 2020-12-31 00:36

    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();
    
    0 讨论(0)
  • 2020-12-31 00:38

    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

    0 讨论(0)
  • 2020-12-31 00:52

    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.

    0 讨论(0)
提交回复
热议问题