I've tried everything I can think of and can't get this
AnimationDrawable (from code, not XML) to work. Please let me know
what I doing wrong.

First, we have the regular simple app:

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));
   }
}

And the view class this uses:

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);

    }

    // 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();
        }
    }
}

I have verified that the animation is indeed running, that it is
looping, 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.

Any insight into why I am only seeing the first frame and can't get
this to animate would be greatly appreciated.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to