Getting Animation Drawables to actually Animate

Quickly share your Android Code Snippets here...

Getting Animation Drawables to actually Animate

Postby FreeKill » Wed Oct 15, 2008 11:18 pm

I had to do a lot of research because the details on how to do this don't seem to work with the newest build as is in the google documentation listed here (http://code.google.com/android/referenc ... wable.html).

splash_animation.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.         android:id="@+id/splashAnimation"
  7.  
  8.         android:oneshot="false">
  9.  
  10.                 <item android:drawable="@drawable/splash1" android:duration="3000" />
  11.  
  12.                 <item android:drawable="@drawable/splash2" android:duration="50" />
  13.  
  14.                 <item android:drawable="@drawable/splash3" android:duration="50" />
  15.  
  16.                 <item android:drawable="@drawable/splash4" android:duration="50" />
  17.  
  18.                 <item android:drawable="@drawable/splash5" android:duration="50" />
  19.  
  20.                 <item android:drawable="@drawable/splash6" android:duration="50" />
  21.  
  22.                 <item android:drawable="@drawable/splash7" android:duration="50" />
  23.  
  24.                 <item android:drawable="@drawable/splash8" android:duration="50" />
  25.  
  26.                 <item android:drawable="@drawable/splash9" android:duration="50" />
  27.  
  28.                 <item android:drawable="@drawable/splash10" android:duration="50" />
  29.  
  30.                 <item android:drawable="@drawable/splash11" android:duration="50" />
  31.  
  32.                 <item android:drawable="@drawable/splash12" android:duration="50" />
  33.  
  34.                 <item android:drawable="@drawable/splash13" android:duration="50" />
  35.  
  36.                 <item android:drawable="@drawable/splash14" android:duration="50" />
  37.  
  38.                 <item android:drawable="@drawable/splash15" android:duration="5000" />
  39.  
  40. </animation-list>
  41.  
  42.  
Parsed in 0.006 seconds, using GeSHi 1.0.8.4


This would go somewhere in your code, like the onCreate function if you want it to fire off when your app starts

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. ImageView animation = (ImageView) findViewById( R.id.splashAnimation );
  3.  
  4. animation.setBackgroundResource( R.drawable.splash_animation );
  5.  
  6.        
  7.  
  8. AnimationRoutine animationRoutine = new AnimationRoutine( );
  9.  
  10.        
  11.  
  12. Timer t = new Timer( false );
  13.  
  14. t.schedule( animationRoutine, 100 );
  15.  
  16.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. private class AnimationRoutine extends TimerTask {
  3.  
  4.         AnimationRoutine( ) { }
  5.  
  6.                
  7.  
  8.         public void run( ) {
  9.  
  10.                 ImageView img = (ImageView) findViewById( R.id.splashAnimation );
  11.  
  12.                 AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground( );
  13.  
  14.                 frameAnimation.start( );       
  15.  
  16.         }
  17.  
  18. }
  19.  
  20.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Anyway, I hope that helps someone, I was having a hell of a time finding a good way to get it to work...
FreeKill
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Oct 15, 2008 11:05 pm

Top

Postby ninor » Thu Oct 16, 2008 5:39 am

Thanks for sharing! :)
Image AndDev: Your Android Development Community / Tutorials | Here's my Basic ToolKit
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Postby pwhisler » Tue Nov 11, 2008 8:15 am

it crashes on the:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. animation.setBackgroundResource( R.drawable.animation );
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


it says: "The application () has stopped unexpectedly. Please try again." with a button for Force Close

anyone know why?

thanks
pwhisler
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sat Nov 08, 2008 8:32 pm

Postby MrSnowflake » Tue Nov 11, 2008 11:32 am

CHECK YOU LOG!!!
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby pwhisler » Tue Nov 11, 2008 8:35 pm

ok, checking my LogCat, it says there is a runtime uncaught null pointer exception at the line:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. img.setBackgroundResource(R.drawable.animation);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


but I don't know why, because the animation.xml file is in my drawable folder, there are no red squiggly lines any where, it compiles no problem.
pwhisler
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sat Nov 08, 2008 8:32 pm

Postby pwhisler » Tue Nov 11, 2008 8:56 pm

ok, i guess the problem is that onDraw gets called before onCreate? so if i put a
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. if(img!=null)
  3.  
  4.                 img.setBackgroundResource(R.drawable.animation);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


then it doesn't crash
pwhisler
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sat Nov 08, 2008 8:32 pm

Top

Postby pwhisler » Tue Nov 11, 2008 10:09 pm

but, I don't see anything, I used:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. img.draw(canvas);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


to draw the imageview in the doDraw() method, but I don't see anything, how do you make it show up on the canvas?
pwhisler
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sat Nov 08, 2008 8:32 pm

Postby amitonrise » Wed Nov 19, 2008 6:31 am

can anyone tell me how to continously repeat a event after a certain peroid of time ???
amitonrise
Freshman
Freshman
 
Posts: 3
Joined: Mon Nov 10, 2008 6:58 am

animating child view rows of my list.

Postby mikpg » Wed Dec 10, 2008 12:20 am

I am having a problem animating child view rows of my list. When I animate the child view list I have a repopulation conflict with each other. I get a crash with a nullpointer exception in the getView method of my list adapter. Already tried repopulating the list in the onAnimationEnd method in an animation listener. When I kill the animation my http request works fine any suggestions would be greatly appreciated
Mikpg
mikpg
Freshman
Freshman
 
Posts: 5
Joined: Tue Nov 11, 2008 4:02 pm
Location: null

Postby doubleslash » Mon Jun 22, 2009 6:30 am

I too have gotten a null pointer exception, but I fixed it as follows.You need to call setContentView before calling findViewById because setContentView will create the views, so that when you call findViewById, you'll get a reference to an existing object (imageView). So, before you follow FreeKill's code, call setContentView(R.layout.main), where main is an xml file that contains a layout and an imageview that is to host your animation.
I don't understand why you have to go through TimerTask, as done by FreeKill, to get it to work though. I also tried the Google's example, but it only showed the first frame. Can anyone explain this?
Thanks
doubleslash
Developer
Developer
 
Posts: 30
Joined: Fri Feb 13, 2009 6:05 am

Postby Shanytc » Sat Aug 15, 2009 1:01 pm

Hey, I got it to work just fine,
however, the animation is displayed on the top left (0,0) side of the screen,
how can I change the position of the ImageView position on the screen?
I would like to move the animation to the middle of the screen
- Shany -
Shanytc
Junior Developer
Junior Developer
 
Posts: 17
Joined: Fri Aug 14, 2009 11:00 am

Postby pmskiran » Wed Aug 19, 2009 7:31 am

Hi
Shanytc,

If u want the image animation in center u can use like this...

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    android:orientation="vertical"
  6.  
  7.    android:layout_width="fill_parent"
  8.  
  9.    android:layout_height="fill_parent"
  10.  
  11.    >
  12.  
  13.  
  14.  
  15.     <ImageView
  16.  
  17.         android:id="@+id/splashAnimation"
  18.  
  19.         android:layout_width="wrap_content"
  20.  
  21.         android:layout_height="wrap_content"
  22.  
  23.         android:layout_gravity="center_vertical|center_horizontal"
  24.  
  25.    ></ImageView>
  26.  
  27. </FrameLayout>
  28.  
  29.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
Kiran PMS
pmskiran
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri Jul 31, 2009 10:44 am
Location: India

Re: Getting Animation Drawables to actually Animate

Postby pskink » Wed Aug 19, 2009 11:12 am

FreeKill wrote:I had to do a lot of research because the details on how to do this don't seem to work with the newest build as is in the google documentation listed here (http://code.google.com/android/referenc ... wable.html).

...

Anyway, I hope that helps someone, I was having a hell of a time finding a good way to get it to work...


no, no and again no!

AnimationDrawable animates by itself, you dont have to use any Timer nor TimerTask.

just do in onCreate:

view.setBackgroundResource(animId)

and later when view is *visible* (not in onCreate):

AnimationDrawable ad = (AnimationDrawable) view.getBackground()

ad.start()

thats it
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby ranjit0046 » Tue Feb 02, 2010 10:16 am

amitonrise wrote:can anyone tell me how to continously repeat a event after a certain peroid of time ???



Hi friend,

My problem is little bit same like your problem, i need to start apk file after just installation with out any user interaction ,if you know please tell me , how it is....

Astel...
ranjit0046
Developer
Developer
 
Posts: 47
Joined: Mon Dec 14, 2009 12:46 pm

Re: Getting Animation Drawables to actually Animate

Postby ranjit0046 » Tue Feb 02, 2010 10:18 am

Hi friend ,


how to invoke an apk file after installation with out any user interaction (automatically) if you know please tell me ...

Astel....





FreeKill wrote:I had to do a lot of research because the details on how to do this don't seem to work with the newest build as is in the google documentation listed here (http://code.google.com/android/referenc ... wable.html).

splash_animation.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:id="@+id/splashAnimation"
  4.         android:oneshot="false">
  5.                 <item android:drawable="@drawable/splash1" android:duration="3000" />
  6.                 <item android:drawable="@drawable/splash2" android:duration="50" />
  7.                 <item android:drawable="@drawable/splash3" android:duration="50" />
  8.                 <item android:drawable="@drawable/splash4" android:duration="50" />
  9.                 <item android:drawable="@drawable/splash5" android:duration="50" />
  10.                 <item android:drawable="@drawable/splash6" android:duration="50" />
  11.                 <item android:drawable="@drawable/splash7" android:duration="50" />
  12.                 <item android:drawable="@drawable/splash8" android:duration="50" />
  13.                 <item android:drawable="@drawable/splash9" android:duration="50" />
  14.                 <item android:drawable="@drawable/splash10" android:duration="50" />
  15.                 <item android:drawable="@drawable/splash11" android:duration="50" />
  16.                 <item android:drawable="@drawable/splash12" android:duration="50" />
  17.                 <item android:drawable="@drawable/splash13" android:duration="50" />
  18.                 <item android:drawable="@drawable/splash14" android:duration="50" />
  19.                 <item android:drawable="@drawable/splash15" android:duration="5000" />
  20. </animation-list>
  21.  
Parsed in 0.007 seconds, using GeSHi 1.0.8.4


This would go somewhere in your code, like the onCreate function if you want it to fire off when your app starts

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ImageView animation = (ImageView) findViewById( R.id.splashAnimation );
  2. animation.setBackgroundResource( R.drawable.splash_animation );
  3.        
  4. AnimationRoutine animationRoutine = new AnimationRoutine( );
  5.        
  6. Timer t = new Timer( false );
  7. t.schedule( animationRoutine, 100 );
  8.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private class AnimationRoutine extends TimerTask {
  2.         AnimationRoutine( ) { }
  3.                
  4.         public void run( ) {
  5.                 ImageView img = (ImageView) findViewById( R.id.splashAnimation );
  6.                 AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground( );
  7.                 frameAnimation.start( );       
  8.         }
  9. }
  10.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Anyway, I hope that helps someone, I was having a hell of a time finding a good way to get it to work...
ranjit0046
Developer
Developer
 
Posts: 47
Joined: Mon Dec 14, 2009 12:46 pm

Top
Next

Return to Code Snippets for Android

Who is online

Users browsing this forum: Yahoo [Bot] and 5 guests