One major issue I ran into when working on the new Manning book, Unlocking Android, was trying to use the Google Android Frame by Frame animation example and API from Google.  It appears to be outdated and several people seemed to be having this issue.  Frank Abelson, the main author, lent me a hand and updated my code to work well.  Here is the source so hopefully others can overcome their problems with the API (note I am using the latest version of the SDK (m5-rc15). Anyway, here is the code:

 simple_animation.xml

 <?xml version=”1.0″ encoding=”utf-8″?>
<animation-list xmlns:android=”http://schemas.android.com/apk/res/android” id=”selected” android:oneshot=”false”>
<item android:drawable=”@drawable/ball1″ android:duration=”50″ />
<item android:drawable=”@drawable/ball2″ android:duration=”50″ />
<item android:drawable=”@drawable/ball3″ android:duration=”50″ />
<item android:drawable=”@drawable/ball4″ android:duration=”50″ />
<item android:drawable=”@drawable/ball5″ android:duration=”50″ />
<item android:drawable=”@drawable/ball6″ android:duration=”50″ />
</animation-list>

 Main.xml

<?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”
>
<ImageView android:id=”@+id/simple_anim”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:gravity=”center”
android:layout_centerHorizontal=”true”
/>
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Hello World, XMLAnimation”
/>
</LinearLayout>

 

XMLAnimation.java 

package com.msi.manning.chapter10.xmlanimation;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
import java.util.Timer;
import java.util.TimerTask;

public class XMLAnimation extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

ImageView img = (ImageView)findViewById(R.id.simple_anim);
img.setBackground(R.anim.simple_animation);

MyAnimationRoutine mar = new MyAnimationRoutine();
MyAnimationRoutine2 mar2 = new MyAnimationRoutine2();

Timer t = new Timer(false);
t.schedule(mar, 100);
Timer t2 = new Timer(false);
t2.schedule(mar2, 5000);

}

class MyAnimationRoutine extends TimerTask
{
MyAnimationRoutine()
{
}

public void run()
{
ImageView img = (ImageView)findViewById(R.id.simple_anim);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();

// Start the animation (looped playback by default).
frameAnimation.start();
}
}

class MyAnimationRoutine2 extends TimerTask
{
MyAnimationRoutine2()
{
}

public void run()
{
ImageView img = (ImageView)findViewById(R.id.simple_anim);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();

// stop the animation (looped playback by default).
frameAnimation.stop();
}
}
}

 

Posted In:
Author: Robi Sen