sai
sai

Reputation: 2562

how to start Animation in onCreate() in android?

I have done like these it not working?

public class AnimationActivity extends Activity{
            private AnimationDrawable yourAnimation; 
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.animation);  
                getIntentValues();
                final ImageView imageView = (ImageView) findViewById(R.id.animation_iv);   
                imageView.setBackgroundResource(R.drawable.loadinganim);   
                yourAnimation = (AnimationDrawable) imageView.getBackground(); 
                yourAnimation.start();
    }

res\drawable\loading.xml

<?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/v1" android:duration="160" />  
    <item android:drawable="@drawable/v4" android:duration="160" />  
    <item android:drawable="@drawable/v7" android:duration="160" />  
    <item android:drawable="@drawable/v8" android:duration="160" />  
    <item android:drawable="@drawable/v9" android:duration="160" />  
    <item android:drawable="@drawable/v10" android:duration="160" />  
    <item android:drawable="@drawable/v11" android:duration="160" />  
   </animation-list>

res\layout\animation.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
    >
<ImageView 
 android:id="@+id/animation_iv"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" 
 />

Animation is starting on imageView onClick().if any one have idea help me.

Upvotes: 1

Views: 7666

Answers (6)

ShenStillTheMain
ShenStillTheMain

Reputation: 1

In Kotlin I used

    view.doOnAttach{
        //your Animation
    }

Upvotes: 0

ray liu
ray liu

Reputation: 57

onEnterAnimationComplete()

"Activities cannot draw during the period that their windows are animating in. In order to know when it is safe to begin drawing they can override this method which will be called when the entering animation has completed".

You can override this method and start your animation in this method.

Upvotes: 0

Praveenkumar
Praveenkumar

Reputation: 24506

Yes, your method is right but, you need to implement this -

public void onWindowFocusChanged (boolean hasFocus) 
{
    super.onWindowFocusChanged(hasFocus);
    AnimationDrawable yourAnimation = 
        (AnimationDrawable) animation.getBackground();
    if(hasFocus) {
        frameAnimation.start();
    } else {
        frameAnimation.stop();
    }
}

like in this example

Upvotes: 3

Janusz
Janusz

Reputation: 189594

It seems that while the onCreate method is running the Drawable is not fully initialized and therefore starting the animation will not work.

Start your animation in the onWindowFocusChanged method. This is sadly only documented in the Animation Drawable Guide and not in the corresponding Java Docs.

I filed a Documentation Bug on this issue. Star the issue if you want the android team to improve their documentation.

Upvotes: 1

amukhachov
amukhachov

Reputation: 5900

Seems like you are doing wrong cast.Your background is StateListDrawable, not AnimationDrawable. There is similar question here . Hope it'll be helpful.

Upvotes: 0

Hannibal Smith
Hannibal Smith

Reputation: 36

          view.setImageResource(R.anim.framer);
          view.post(new Runnable() {
            public void run() {             
                AnimationDrawable animationDrawable = (AnimationDrawable)view.getDrawable();
                animationDrawable.start();
            }
          });

This worked for me try it !!!!

Upvotes: 2

Related Questions