1. Check out our app, Forums for Android! Download and leave feedback here!

Animated Sprite Using XML?

Discussion in 'Application Development' started by safibaba, May 4, 2010.

  1. safibaba

    safibaba Member
    Thread Starter
    15

    May 3, 2010
    15
    0
    15
    Hi all

    The Android docs shows a way to create an XML definition of an animation like this:

    PHP:
    1. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    2.     android:oneshot="false">
    3.     <item android:drawable="@drawable/img1" android:duration="200" />
    4.     <item android:drawable="@drawable/img2" android:duration="200" />
    5.     <item android:drawable="@drawable/img3" android:duration="200" />
    6. </animation-list>
    I have been trying to create a Drawable from this XML like this

    PHP:
    1. Drawable sprite = (Drawable) getResources().getDrawable(R.anim.sprite);
    And then draw it on my canvas in my SurfaceView like this

    PHP:
    1. sprite.draw(canvas);
    It draws the first image - but it doesn't animate through the images specified in the XML.

    I thought maybe I needed to make it an AnimationDrawable like this

    PHP:
    1. AnimationDrawable sprite = (AnimationDrawable) getResources().getAnimation(R.anim.sprite);
    But it still didn't animate. So finally I thought maybe I need to call start() on it, but that gives me an error.

    Am I just barking up the wrong tree here? Is it not possible to draw an animation to a canvas in this way? I have seen a tutorial which uses a sprite sheet and the setbounds method to do this, so maybe it's just not possible using the XML route?
     

    Advertisement

  2. Boogs

    Boogs Well-Known Member
    18

    Apr 6, 2010
    85
    16
    18
    You're not trying to call start in onCreate() are you?
     
  3. safibaba

    safibaba Member
    Thread Starter
    15

    May 3, 2010
    15
    0
    15
    Hi Boogs - actually yes I was. I didn't know I shouldn't be :eek:.

    So is this a valid way to draw animations when using a SurfaceView?

    HOWEVER - the start() thing doesn't seem to be where it falls over, because I think it doesn't even get that far.

    I am getting a classcastexception here

    PHP:
    1. AnimationDrawable sprite = (AnimationDrawable) getResources().getAnimation(R.anim.sprite);
    I don't suppose you know where I could find an example of doing this correctly?
     
  4. Boogs

    Boogs Well-Known Member
    18

    Apr 6, 2010
    85
    16
    18
    Well, it seems usually calling animation "start" or similar calls in onCreate can be problematic because all the views haven't finished their "setup". Check out the note at the bottom of the Android developer 2D graphics page which basically says the same thing.

    As for an example, I don't have any great examples for you. But, you might want to check out the Android examples Jet Boy or Lunar Lander for some examples of drawing on a canvas SurfaceView. Then integrate your AnimationDrawable in a similar manner.

    Boogs
     
    alostpacket likes this.
  5. safibaba

    safibaba Member
    Thread Starter
    15

    May 3, 2010
    15
    0
    15
    Thanks for the tips Boogs..but I just can't get it to work at all.

    I tried calling start from a separate thread, even calling start() from an onTouchEvent to make sure it gets called after the onCreate is finished..but I just can't get this damn thing to animate...

    Is it something to do with the setBounds call?

    res/anim/sprite.xml
    PHP:
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:oneshot="false">
    4.     <item android:drawable="@drawable/sprite1" android:duration="500" />
    5.     <item android:drawable="@drawable/sprite2" android:duration="500" />
    6.     <item android:drawable="@drawable/sprite3" android:duration="500" />
    7.     <item android:drawable="@drawable/sprite4" android:duration="500" />
    8.     <item android:drawable="@drawable/sprite5" android:duration="500" />
    9. </animation-list>
    SurfaceView
    PHP:
    1.  
    2. public void surfaceCreated(SurfaceHolder holder) {
    3.  sprite = (AnimationDrawable) getResources().getDrawable(R.anim.sprite);
    4.  sprite.setBounds(0,0,sprite.getIntrinsicWidth(),sprite.getIntrinsicHeight());
    5. }
    6.  
    7. public void startAnim(){
    8.    Log.v("APPLET","Start Anim");
    9.    sprite.start();
    10. }
    11.  
    Main Activity class (which creates surface view)

    PHP:
    1.  
    2. @Override
    3. public boolean onTouchEvent(MotionEvent event){
    4.    gameScreen.startAnim();
    5.    return true;
    6. }
    7.  
    It outputs the log message fine, but the animation never starts So frustrating :(
     
  6. Boogs

    Boogs Well-Known Member
    18

    Apr 6, 2010
    85
    16
    18
    Safibaba,

    Sorry that didn't work. To be honest, I don't know why it won't work. My only suggestion would be to put your animation into an ImageView in an Activity and see if that works. If it does, then at least you know that your XML file and the like are working fine...
     
  7. safibaba

    safibaba Member
    Thread Starter
    15

    May 3, 2010
    15
    0
    15
    I fixed it Boogs - I find the docs a bit obscure sometimes ...after googling for the best part of a day..it turned out I was missing the addCallback() method to make the parent thread schedule updates to the animation. I thought the animation handled its own frame updates!!
     
  8. Boogs

    Boogs Well-Known Member
    18

    Apr 6, 2010
    85
    16
    18
    Wow, it seems like they should've made that easier, or at least documented it better if it's that hard. Either way, I'm glad it worked out for you.
     
  9. anserran

    anserran New Member
    5

    Jun 23, 2010
    1
    0
    5
    Hi, safibaba

    I have exactly the same problem... How did you add that callback? I've been stuck with this for 2 days...

    Thanks!
     
  10. Pabrick

    Pabrick Member
    36

    Sep 5, 2011
    12
    2
    36
    Use this:
    AnimationDrawable sprite = (AnimationDrawable) sprite.Background(R.anim.sprite);
     
    alostpacket likes this.

Share This Page

Loading...