Help with AnimationDrawable

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Help with AnimationDrawable

Postby Ravenize » Thu Jan 22, 2009 9:55 am

Hey all.

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:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Example extends Activity {
  2.  
  3.  
  4.  
  5.    /** Called when the activity is first created. */
  6.  
  7.    @Override
  8.  
  9.    public void onCreate(Bundle savedInstanceState) {
  10.  
  11.        super.onCreate(savedInstanceState);
  12.  
  13.  
  14.  
  15.        setContentView(new ExampleView(this));
  16.  
  17.    }
  18.  
  19.  
  20.  
  21. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


And the view class this uses:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. class ExampleView extends View {
  3.  
  4.  
  5.  
  6.     private Bitmap image1;
  7.  
  8.     private Bitmap image2;
  9.  
  10.     private BitmapDrawable frame1;
  11.  
  12.     private BitmapDrawable frame2;
  13.  
  14.     private AnimationDrawable anim;
  15.  
  16.  
  17.  
  18.     public ExampleView(Context context) {
  19.  
  20.         super(context);
  21.  
  22.  
  23.  
  24.         Resources res = context.getResources();
  25.  
  26.  
  27.  
  28.         anim = new AnimationDrawable();
  29.  
  30.         image1 = BitmapFactory.decodeResource(res, R.drawable.image1);
  31.  
  32.         image2 = BitmapFactory.decodeResource(res, R.drawable.image2);
  33.  
  34.         frame1 = new BitmapDrawable(image1);
  35.  
  36.         frame2 = new BitmapDrawable(image2);
  37.  
  38.         anim.addFrame(frame1, 50);
  39.  
  40.         anim.addFrame(frame2, 50);
  41.  
  42.         anim.setOneShot(Boolean.FALSE); // make it loop
  43.  
  44.     }
  45.  
  46.  
  47.  
  48.     @Override
  49.  
  50.     protected void onDraw(Canvas canvas) {
  51.  
  52.  
  53.  
  54.        // draw a bunch of other stuff
  55.  
  56.  
  57.  
  58.         anim.setBounds(0, 0, 50, 50);
  59.  
  60.         anim.draw(canvas);
  61.  
  62.  
  63.  
  64.         invalidate();
  65.  
  66.     }
  67.  
  68.  
  69.  
  70.     // fix for bug where .start() doesn't work within context of activity starting
  71.  
  72.     @Override
  73.  
  74.     public void onWindowFocusChanged(boolean has_focus) {
  75.  
  76.         if (has_focus) {
  77.  
  78.             anim.start();
  79.  
  80.         } else {
  81.  
  82.             anim.stop();
  83.  
  84.         }
  85.  
  86.     }
  87.  
  88.  
  89.  
  90. }
Parsed in 0.035 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.
Ravenize
Freshman
Freshman
 
Posts: 3
Joined: Thu Jan 22, 2009 9:44 am

Top

Postby Ravenize » Sat Feb 07, 2009 9:35 pm

If anyone else is still looking at this I finally figured it out. The trick is to use a ViewGroup as the parent class and then use addView to attach something like an ImageView that has the AnimationDrawable set as its background. It might seem clunky at first, but this gives you the ability to add as many Views / Animations as you want and each one controls its own drawing.
Ravenize
Freshman
Freshman
 
Posts: 3
Joined: Thu Jan 22, 2009 9:44 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 2 guests