Starting Frame-By-Frame Animation

后端 未结 3 1222
野趣味
野趣味 2020-11-27 07:14

I have a basic question about starting a frame-by-frame animation.

When I call the AnimationDrawable.start() method from my code directly, it doesn\

相关标签:
3条回答
  • 2020-11-27 07:32

    to play an animation just in onCreate(...) add:

    ImageView mImageView=(ImageView) findViewById(R.id.image);          
    mImageView.setBackgroundResource(R.anim.film);    
    mFrameAnimation = (AnimationDrawable) mImageView.getBackground();    
    mImageView.post(new Runnable(){    
        public void run(){    
            mFrameAnimation.start();        
    }
    });
    
    0 讨论(0)
  • 2020-11-27 07:41

    Use a Runnable to insert the start() message to message queue, just add this LOC to replace your mFrameAnimation.start();

    img.post(new Starter());
    

    Helper inner class:

    class Starter implements Runnable {
            public void run() {
                mFrameAnimation.start();
            }
    }
    
    0 讨论(0)
  • 2020-11-27 07:47

    It's important to note that the start() method called on the AnimationDrawable cannot be called during the onCreate() method of your Activity, because the AnimationDrawable is not yet fully attached to the window. If you want to play the animation immediately, without requiring interaction, then you might want to call it from the onWindowFocusChanged() method in your Activity, which will get called when Android brings your window into focus. Very end of the page http://developer.android.com/guide/topics/graphics/2d-graphics.html

     ImageView img = (ImageView)findViewById(R.id.some layout);
     AnimationDrawable frameAnimation =    (AnimationDrawable)img.getDrawable();
     frameAnimation.setCallback(img);
     frameAnimation.setVisible(true, true);
     frameAnimation.start();
    

    and to add animation you can do something like

    <animation-list   android:id="@+id/my_animation" android:oneshot="false" 
        xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/frame1" android:duration="150" />
        <item android:drawable="@drawable/frame2" android:duration="150" />
    
     </animation-list>  
    
    0 讨论(0)
提交回复
热议问题