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:
the main.xml file is:
cheers,
cLight
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();
}
}
}
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>
cLight
