how to start Animation in onCreate() in android?

2019-02-20 08:27发布

问题:

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.

回答1:

          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 !!!!



回答2:

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



回答3:

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.



回答4:

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



回答5:

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.