Now I know there are three ways:
The code is as follows |
Copy Code |
ImageView ImageView = (imageview) Findviewbyid (r.id.xxx); method One: Use Runnalbe () to load Imageview.setbackgroundresource (R.ANIM.XXXXX); Final animationdrawable animationdrawable = (animationdrawable) imageview.getbackground (); Imageview.post (New Runnable () { @Override public void Run () { Animationdrawable.start (); } }); |
Method Two: Start with asynctask asynchronous loading
The code is as follows |
Copy Code |
Imageview.setbackgroundresource (R.ANIM.XXXXX); Animationdrawable animationdrawable = (animationdrawable) imageview.getbackground (); Runanim runanim=new Runanim (); Runanim.execute (""); &NBSP Class Runanim extends Asynctask<string, String, string> { @Override protected string Doinbackground (String ... params) { if (!animationdrawable.isrunning ( ) { Animationdrawable.stop (); Animationdrawable.start (); } return ""; } } |
Method Three: From dynamic loading by adding Addonpredrawlistener
code is as follows |
copy code |
Imageview.setba Ckgroundresource (R.ANIM.XXXXX); Animationdrawable animationdrawable = (animationdrawable) imageview.getbackground (); Imageview.getviewtreeobserver (). Addonpredrawlistener (Predrawlistener); Onpredrawlistener predrawlistener = new Onpredrawlistener () { @Override public boolean Onpredraw () { Animationdrawable.start (); return true;///must have this true returns } }; |
The above three methods have been tested and there is no problem, and there are some online use of rewriting the activity of the onwindowfocuschanged () method to achieve, but there is still insufficient, have to change the focus to trigger, although the theory can automatically achieve change focus, feeling or not very desirable.