Go Back   Android Forums > Android Development > Application Development

Application Development Dev Lounge for the Coder Folks



Reply
 
LinkBack Thread Tools
Old May 4th, 2010, 07:19 AM   #1 (permalink)
Junior Member
 
Join Date: May 2010
Posts: 15
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default Animated Sprite Using XML?

Hi all

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

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

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

PHP Code:
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 Code:
AnimationDrawable sprite = (AnimationDrawablegetResources().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?

safibaba is offline  
Reply With Quote
Sponsors
Old May 4th, 2010, 07:39 AM   #2 (permalink)
Junior Member
 
Join Date: Apr 2010
Posts: 86
 
Device(s):
Thanks: 0
Thanked 16 Times in 15 Posts
Default

You're not trying to call start in onCreate() are you?
Boogs is offline  
Reply With Quote
Old May 4th, 2010, 07:52 AM   #3 (permalink)
Junior Member
 
Join Date: May 2010
Posts: 15
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Boogs - actually yes I was. I didn't know I shouldn't be .

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 Code:
AnimationDrawable sprite = (AnimationDrawablegetResources().getAnimation(R.anim.sprite); 
I don't suppose you know where I could find an example of doing this correctly?
safibaba is offline  
Reply With Quote
Old May 4th, 2010, 08:38 AM   #4 (permalink)
Junior Member
 
Join Date: Apr 2010
Posts: 86
 
Device(s):
Thanks: 0
Thanked 16 Times in 15 Posts
Default

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
Boogs is offline  
Reply With Quote
The Following User Says Thank You to Boogs For This Useful Post:
alostpacket (September 12th, 2011)
Old May 4th, 2010, 06:43 PM   #5 (permalink)
Junior Member
 
Join Date: May 2010
Posts: 15
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 Code:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/sprite1" android:duration="500" />
    <item android:drawable="@drawable/sprite2" android:duration="500" />
    <item android:drawable="@drawable/sprite3" android:duration="500" />
    <item android:drawable="@drawable/sprite4" android:duration="500" />
    <item android:drawable="@drawable/sprite5" android:duration="500" />
</animation-list>
SurfaceView
PHP Code:
public void surfaceCreated(SurfaceHolder holder) {
 
sprite = (AnimationDrawablegetResources().getDrawable(R.anim.sprite);
 
sprite.setBounds(0,0,sprite.getIntrinsicWidth(),sprite.getIntrinsicHeight());
}

public 
void startAnim(){
   
Log.v("APPLET","Start Anim");
   
sprite.start();

Main Activity class (which creates surface view)

PHP Code:
@Override
public boolean onTouchEvent(MotionEvent event){
   
gameScreen.startAnim();
   return 
true;

It outputs the log message fine, but the animation never starts So frustrating
safibaba is offline  
Reply With Quote
Old May 5th, 2010, 05:19 PM   #6 (permalink)
Junior Member
 
Join Date: Apr 2010
Posts: 86
 
Device(s):
Thanks: 0
Thanked 16 Times in 15 Posts
Default

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...
Boogs is offline  
Reply With Quote
Old May 6th, 2010, 04:11 PM   #7 (permalink)
Junior Member
 
Join Date: May 2010
Posts: 15
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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!!
safibaba is offline  
Reply With Quote
Old May 6th, 2010, 05:14 PM   #8 (permalink)
Junior Member
 
Join Date: Apr 2010
Posts: 86
 
Device(s):
Thanks: 0
Thanked 16 Times in 15 Posts
Default

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.
Boogs is offline  
Reply With Quote
Old June 23rd, 2010, 02:14 PM   #9 (permalink)
New Member
 
Join Date: Jun 2010
Posts: 1
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, safibaba

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

Thanks!
anserran is offline  
Reply With Quote
Old September 6th, 2011, 07:19 AM   #10 (permalink)
New Member
 
Join Date: Sep 2011
Posts: 12
 
Device(s):
Thanks: 3
Thanked 2 Times in 2 Posts
Default

Use this:
AnimationDrawable sprite = (AnimationDrawable) sprite.Background(R.anim.sprite);
Pabrick is offline  
Reply With Quote
The Following User Says Thank You to Pabrick For This Useful Post:
alostpacket (September 12th, 2011)
Sponsors
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
When to use a sprite sheet? Yratilos Application Development 3 September 30th, 2010 03:24 AM
MyBackupPro or Sprite Pro kgreen66 HTC EVO 4G 1 August 10th, 2010 05:00 AM
MyBackup or Sprite? Ziani Android Applications 3 February 1st, 2010 12:28 PM
Sprite Backup does not work... Michaelvg1 HTC Hero (Sprint) 2 January 23rd, 2010 09:28 AM
Animated sprite style ImageView gostaf` Android Games 0 August 5th, 2009 05:29 AM



All times are GMT -5. The time now is 03:16 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo


SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.