1

I have created animation.xml file in which i have added my stuff of frame animation but it is not working i have called 5this in my activity class as follow....

animation = (ImageView)findViewById(R.id.imageAnimation);
        animation.setBackgroundResource(R.drawable.animation);      // the frame-by-frame animation defined as a xml file within the drawable folder
        AnimationDrawable frameAnimation = 
                (AnimationDrawable) animation.getBackground();
            frameAnimation.start();

can any one tell me what is problem in that

animation.xmal file

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="false">

    <item android:drawable="@drawable/anatomy_5" android:duration="50" />
    <item android:drawable="@drawable/anatomy_6" android:duration="50" />
    <item android:drawable="@drawable/anatomy_7" android:duration="50" />
    <item android:drawable="@drawable/anatomy_8" android:duration="50" />
    <item android:drawable="@drawable/anatomy_9" android:duration="50" />
    <item android:drawable="@drawable/anatomy_10" android:duration="50" />
    <item android:drawable="@drawable/anatomy_11" android:duration="50" />
    <item android:drawable="@drawable/anatomy_12" android:duration="50" />
    <item android:drawable="@drawable/anatomy_13" android:duration="50" />
    <item android:drawable="@drawable/anatomy_14" android:duration="50" />
    <item android:drawable="@drawable/anatomy_15" android:duration="50" />
    <item android:drawable="@drawable/anatomy_16" android:duration="50" />
    <item android:drawable="@drawable/anatomy_17" android:duration="50" />
    <item android:drawable="@drawable/anatomy_18" android:duration="50" />
    <item android:drawable="@drawable/anatomy_19" android:duration="50" />
    <item android:drawable="@drawable/anatomy_20" android:duration="50" />
    <item android:drawable="@drawable/anatomy_21" android:duration="50" />
    <item android:drawable="@drawable/anatomy_22" android:duration="50" />
    <item android:drawable="@drawable/anatomy_23" android:duration="50" />
    <item android:drawable="@drawable/anatomy_24" android:duration="50" />
    <item android:drawable="@drawable/anatomy_25" android:duration="50" />
    <item android:drawable="@drawable/anatomy_26" android:duration="50" />
    <item android:drawable="@drawable/anatomy_27" android:duration="50" />
    <item android:drawable="@drawable/anatomy_28" android:duration="50" />
    <item android:drawable="@drawable/anatomy_29" android:duration="50" />
    <item android:drawable="@drawable/anatomy_30" android:duration="50" />
    <item android:drawable="@drawable/anatomy_31" android:duration="50" />
    <item android:drawable="@drawable/anatomy_32" android:duration="50" />
    <item android:drawable="@drawable/anatomy_33" android:duration="50" />
    <item android:drawable="@drawable/anatomy_34" android:duration="50" />
    <item android:drawable="@drawable/anatomy_35" android:duration="50" />
    <item android:drawable="@drawable/anatomy_36" android:duration="50" />
    <item android:drawable="@drawable/anatomy_37" android:duration="50" />
    <item android:drawable="@drawable/anatomy_38" android:duration="50" />
    <item android:drawable="@drawable/anatomy_39" android:duration="50" />
    <item android:drawable="@drawable/anatomy_40" android:duration="50" />
    <item android:drawable="@drawable/anatomy_41" android:duration="50" />
    <item android:drawable="@drawable/anatomy_42" android:duration="50" />
    <item android:drawable="@drawable/anatomy_43" android:duration="50" />
    <item android:drawable="@drawable/anatomy_44" android:duration="50" />
    <item android:drawable="@drawable/anatomy_45" android:duration="50" />
    <item android:drawable="@drawable/anatomy_46" android:duration="50" />
    <item android:drawable="@drawable/anatomy_47" android:duration="50" />
    <item android:drawable="@drawable/anatomy_48" android:duration="50" />
    <item android:drawable="@drawable/anatomy_49" android:duration="50" />

</animation-list>
1

3 Answers 3

3

As seen at the bottom of drawable animation example page:

It's important to note that the start() method called on the AnimationDrawable cannot be called during the onCreate() method of your Activity, because the AnimationDrawable is not yet fully attached to the window. If you want to play the animation immediately, without requiring interaction, then you might want to call it from the onWindowFocusChanged() method in your Activity, which will get called when Android brings your window into focus.

Hope it helps.. ;) Cheers

1
  • 1
    This is one of the best points given here. Not starting the animation during OnCreate()!
    – Levite
    Dec 15, 2014 at 9:53
1

try this

put your animation.xml file in anim folder

Simple Animation

Animation rAnim = AnimationUtils.loadAnimation(your_context, R.anim.animation);
animation.startAnimation(rAnim);

where animation is your imageview

update

Drawable Animation

    AnimationDrawable rocketAnimation;

    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
      rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
      rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
       final Thread thread = new Thread(new Runnable() 
    {
    @Override
    public void run() 
    { wait(1000);      
     activity.runOnUiThread(new Runnable() {@Override public void run()
    {
     rocketAnimation.start();
    }});
     }
});
thread.start();

    }

    public boolean onTouchEvent(MotionEvent event) {
      if (event.getAction() == MotionEvent.ACTION_DOWN) {
        rocketAnimation.start();
        return true;
      }
      return super.onTouchEvent(event);
    }
8
  • what is act ? can you explain? May 30, 2012 at 7:17
  • 1
    it is throwing me runtime exception as Caused by: java.lang.RuntimeException: Unknown animation name: animation-list May 30, 2012 at 7:27
  • but i want that when my activity start then my animation is also get started .... May 30, 2012 at 8:55
  • then write this code rocketAnimation.start(); in oncreate method May 30, 2012 at 9:08
  • activity.runOnUiThread(new Runnable() {@Override public void run() it is showing me error on this line on activity as undefine variable May 30, 2012 at 9:42
1

make folder anim in res and put your animation.xml in anim folder and than use this way

 Animation anim = AnimationUtils.loadAnimation(this,R.anim.animation);
 imageview.setAnimation(anim); 
 or
 imageview.startAnimation(anim);
1
  • 2
    it is throwing me the java.lang.RuntimeException: Unknown animation name: animation-list May 30, 2012 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.