Programmatic frame by frame animation examle - AnimationDrawable


Last Updated: 2013-12-01 05:50:46
  1. cLight

    cLight New Member

    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 (Text):
    1. public class AnimationTest
    2.  extends Activity {
    3.    
    4.     AnimationDrawable animation;
    5.    
    6.     /** Called when the activity is first created. */
    7.     @Override
    8.     public void onCreate(Bundle savedInstanceState) {
    9.         super.onCreate(savedInstanceState);
    10.         setContentView(R.layout.main);
    11.        
    12.         animation = new AnimationDrawable();
    13.         animation.addFrame(getResources().getDrawable(R.drawable.ball1), 100);
    14.         animation.addFrame(getResources().getDrawable(R.drawable.ball2), 1000);
    15.         animation.addFrame(getResources().getDrawable(R.drawable.ball3), 1000);
    16.         animation.setOneShot(false);
    17.        
    18.         ImageView imageAnim =  (ImageView) findViewById([URL="http://r.id/"]R.id[/URL].img);
    19.         imageAnim.setBackgroundDrawable(animation);
    20.        
    21.         // run the start() method later on the UI thread
    22.         imageAnim.post(new Starter());
    23.        
    24.     }
    25.    
    26.     class Starter implements Runnable {
    27.  
    28.         public void run() {
    29.             animation.start();        
    30.         }
    31.        
    32.  
    33.     }
    34. }
    the main.xml file is:

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="[URL]http://schemas.android.com/apk/res/android[/URL]"
    3.     android:orientation="vertical"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     >
    7. <TextView  
    8.     android:layout_width="wrap_content"
    9.     android:layout_height="wrap_content"
    10.     android:text="@string/hello"
    11.     />
    12.     <ImageView     android:id="@+id/img"
    13.             android:layout_width="154px"
    14.             android:layout_height="217px"
    15.  />
    16. </LinearLayout>
    cheers,
    cLight

    Advertisement
    bernynhell and xmancool like this.
  2. Thank you man, exactly what I needed.

    Even managed to improve it, no need for an extra class:


    Code (Text):
    1. imageAnim.post(animation);
  3. sebasx

    sebasx New Member

    That code and any else animation doesn't work for me :(

    Anybody can tell me why? I use emulator Android 2.1 (API 7)
  4. bernynhell

    bernynhell New Member

    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.
  5. bernynhell

    bernynhell New Member

    sebasx, write down your code so we can check.
  6. pinsipito

    pinsipito New Member

    To create AndroidManifiest????? To livewallpaper??
  7. wasim memon

    wasim memon Active Member

    not working on Ginger bread ?
    please give link for any other tutorial ?

Share This Page