Project development used to animationdrawable, call start does not run, very puzzled. Google searched the next. Record it.
This animationdrawable.start can not be written directly in the Onclick,onstart,onresume, is invalid, can not start the animation, can only be written in such as event monitoring.
Here are a few ways to run animationdrawable.
First: In event monitoring start animationdrawable An example of an event that occurs when a view tree is about to be drawn
== (animationdrawable) iv.getbackground (); Iv.getviewtreeobserver (). Addonpredrawlistener (OPDL); O Npredrawlistener OPDL=new Onpredrawlistener () { @Override public Boolean Onpredraw () { ad.start (); return true // note The value returned by this row }};
The second way to start the animation: (the animation will run automatically when activity starts)
ImageView image =(ImageView) Findviewbyid (R.id.animation_view); Image.setbackgroundresource (r.anim.oldsheep_wait); Animationdrawable=(animationdrawable) image.getbackground (); Runanim Runanim=NewRunanim (); Runanim.execute ("");classRunanimextendsAsynctask<string, String, string>{@Overrideprotectedstring Doinbackground (String ... params) {if(!animationdrawable.isrunning ()) {animationdrawable.stop (); Animationdrawable.start (); } return""; }}
The third way to start the animation: (Automatically run animation when activity starts)
ImageView image = (ImageView) Findviewbyid (R.id.animation_view); Image.setbackgroundresource (R.anim.oldsheep_ Wait); = (animationdrawable) image.getbackground (); Image.post (new Runnable () { @Override publicvoid run () { animationdrawable.start (); } });
The fourth way to start the animation: (the animation will be run automatically when activity starts)
ImageView image = (ImageView) Findviewbyid (R.id.animation_view); Image.setbackgroundresource (R.anim.oldsheep_ Wait); = (animationdrawable) image.getbackground (); @Override publicvoid Onwindowfocuschanged (boolean hasfocus) { animationdrawable.start (); Super . onwindowfocuschanged (Hasfocus); }
This article transferred from: http://blog.csdn.net/liuhanhan512/article/details/7666821
Android-animationdrawable (iii) several ways of running