After the Android program runs, the system generates a main thread, and if you modify the UI in the main thread, it will conflict, so Android recommends using handler to change the UI
1.4 Photos Timing Transform
Mainactivity.java
Public classMainactivityextendsActivity {ImageView imageView1; intIndex=0; int[] pics=New int[]{R.DRAWABLE.PIC1,R.DRAWABLE.PIC2,R.DRAWABLE.PIC3}; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ImageView1=(ImageView) Findviewbyid (R.ID.IMAGEVIEW1); Thread.Start (); } Thread Thread=NewThread () { Public voidrun () {Try { while(true) {Sleep (1000); Hand.sendemptymessage (12345); } } Catch(interruptedexception e) {//TODO Auto-generated catch blockE.printstacktrace (); } } }; Handler Hand=NewHandler () { Public voidhandlemessage (Message msg) {if(msg.what==12345) {Imageview1.setimageresource (Pics[index%3]); Index=index+1; } } }; }
View Code
Android Learning-interface-event-handler