• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Programmatic frame by frame animation examle - AnimationDrawable

cLight

Lurker
Hello,

I think many android developers encountered this problem when dealing with AnimationDrawable.

If I try to start the animation from onCreate() method of my activity class, it won't work and only the first image of the animation is visible.

However, I found a way that you can start the frame by frame animation, from the UI thread. ( Even if you exit your application, when you re-enter, the animation will still work).

The trick is to start the animation in the run() method of a class (for ex. Starter)
that implements Runnable, and then call the post(new Starter()) method of the image
(or other view) which has the background set with your animation. In this way, the start()
method of the AnimationDrawable will run on the same thread (which is the UI thread) as the onCreate() method of the activity, but only after the onCreate() is finished and the Activity is initialized with the layout.

Here is my code:

Code:
public class AnimationTest
 extends Activity {
    
    AnimationDrawable animation;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        animation = new AnimationDrawable();
        animation.addFrame(getResources().getDrawable(R.drawable.ball1), 100);
        animation.addFrame(getResources().getDrawable(R.drawable.ball2), 1000);
        animation.addFrame(getResources().getDrawable(R.drawable.ball3), 1000);
        animation.setOneShot(false);
        
        ImageView imageAnim =  (ImageView) findViewById([URL="http://r.id/"]R.id[/URL].img);
        imageAnim.setBackgroundDrawable(animation);
        
        // run the start() method later on the UI thread
        imageAnim.post(new Starter());
        
    }
    
    class Starter implements Runnable {

        public void run() {
            animation.start();        
        }
        

    }
}
the main.xml file is:

Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[URL]http://schemas.android.com/apk/res/android[/URL]"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <ImageView     android:id="@+id/img"
            android:layout_width="154px" 
            android:layout_height="217px"
 />
</LinearLayout>
cheers,
cLight
 
That code and any else animation doesn't work for me :(

Anybody can tell me why? I use emulator Android 2.1 (API 7)
 
this is probebly the best explanation for frame by frame image switcher for android developers that you can find on the net.
thanks again for it. i was looking for an answer in the past two weeks....
keep on the good posts.

bernyn.
 
Back
Top Bottom