Hello,

 I encountered the same problem as many other android developers when
I tried to start the AnimationDrawable
from the onCreate() method of the activity. 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).

Here is a programmatic example for creating a frame by frame animation
that works correctly. The AnimationDrawable is started on the UI
thread but only after the onCreate() method of the activity is
finished and the activity is initialized with the layout.

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(R.id.img);
        imageAnim.setBackgroundDrawable(animation);

        imageAnim.post(new Starter());

    }

    class Starter implements Runnable {

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

    }
}

and the main.xml layout is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
    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>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to