So I seem to be incapable of getting AnimationDrawable to work at all. It's really frustrating and has eaten up a lot of my time, so I figure I need some other people's eyes on this to tell me what I'm doing wrong. I tried getting the Google Group's help on this, but nobody has bothered to respond, so I'm hoping for more over here.
First, we have the regular simple app:
Using java Syntax Highlighting
- public class Example extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(new ExampleView(this));
- }
- }
Parsed in 0.013 seconds, using GeSHi 1.0.8.4
And the view class this uses:
Using java Syntax Highlighting
- class ExampleView extends View {
- private Bitmap image1;
- private Bitmap image2;
- private BitmapDrawable frame1;
- private BitmapDrawable frame2;
- private AnimationDrawable anim;
- public ExampleView(Context context) {
- super(context);
- Resources res = context.getResources();
- anim = new AnimationDrawable();
- image1 = BitmapFactory.decodeResource(res, R.drawable.image1);
- image2 = BitmapFactory.decodeResource(res, R.drawable.image2);
- frame1 = new BitmapDrawable(image1);
- frame2 = new BitmapDrawable(image2);
- anim.addFrame(frame1, 50);
- anim.addFrame(frame2, 50);
- anim.setOneShot(Boolean.FALSE); // make it loop
- }
- @Override
- protected void onDraw(Canvas canvas) {
- // draw a bunch of other stuff
- anim.setBounds(0, 0, 50, 50);
- anim.draw(canvas);
- invalidate();
- }
- // fix for bug where .start() doesn't work within context of activity starting
- @Override
- public void onWindowFocusChanged(boolean has_focus) {
- if (has_focus) {
- anim.start();
- } else {
- anim.stop();
- }
- }
- }
Parsed in 0.011 seconds, using GeSHi 1.0.8.4
I have verified that the animation is indeed running, and that there are multiple frames, but for some reason I can still only see the first frame (i.e. it never actually animates). I've tried attaching the AnimationDrawable to an ImageView as the background drawable and drawing the ImageView to the canvas, but that didn't seem to help at all. I've also messed around with where .start() is called a lot to no avail. Today I even tried using XML instead of using code but its always the same problem: it shows up on the screen exactly where I expect, but only the first frame ever appears.
Any insight into why I am only seeing the first frame and can't get this to animate would be greatly appreciated.