I am working on a project that involves animating an image using a sequence of images that is generated randomly. Here’s code I have right now that is not working (or at least it does not appear to work):
/**
* Method to display the current sequence
* TODO make an animation for the screen to use
*/
public void displaySeq() {
ad = new AnimationDrawable();
int j = 0;
for(Integer i : sequence) {
switch(i)
{
case Constants.RED:
System.out.printf("%d - RED\n", j);
ad.addFrame(resources.getDrawable(R.drawable.red_circle), 500);
break;
case Constants.BLUE:
System.out.printf("%d - BLUE\n", j);
ad.addFrame(resources.getDrawable(R.drawable.blue_circle), 500);
break;
case Constants.GREEN:
System.out.printf("%d - GREEN\n", j);
ad.addFrame(resources.getDrawable(R.drawable.green_circle), 500);
break;
case Constants.YELLOW:
System.out.printf("%d - YELLOW\n", j);
ad.addFrame(resources.getDrawable(R.drawable.yellow_circle), 500);
break;
default:
System.out.printf("%d - bad color: %d\n", j, i);
ad.addFrame(resources.getDrawable(R.drawable.black_circle), 500);
}
j++;
}
// add a black circle to the end of the animation
ad.addFrame(resources.getDrawable(R.drawable.black_circle), 500);
System.out.println("Number of frames: " + ad.getNumberOfFrames());
indicator.setBackground(ad);
indicator.post(new Starter());
}
class Starter implements Runnable {
@Override
public void run() {
ad.start();
}
}
When I run the application and this method gets called, I see the printout in LogCat, but the indicator does not get animated. Any ideas what is going wrong?
I actually figured out the problem. It was being caused by an error in my XML file. Basically, when I made the layout.xml file in Eclipse and added the ImageView (
indicator), Eclipse set theandroid:srcparameter to the image I had selected. Changingandroid:srctoandroid:drawableresolved the problem.