android - how to change images on imageView after some interval -
i have problem want paste images on imageview in android , images periodically changed after interval. means 1 one images shown in imageview. doing of thread in java got problem thread not attached , something. please review code given below , tell me exact error , how remove error or give me diffrent way doing this.
package com.ex.thread; import com.ex.thread.r; import android.app.activity; import android.os.bundle; import android.util.log; import android.widget.imageview; public class thread extends activity implements runnable{ /** called when activity first created. */ public static integer[] mthumbids = { r.drawable.al1,r.drawable.al2,r.drawable.al3,r.drawable.al4, }; thread th; imageview iv; public void run() { for(int i=0;i<3;i++) { iv.setimageresource(mthumbids[i]); system.out.println("sanat pandey"); try{ thread.sleep(3000); }catch(exception e) { system.out.println(e); } } } public void create() { thread th = new thread(new thread()); th.start(); try{ thread.sleep(3000); }catch(exception e) { system.out.println(e); } } @override public void oncreate(bundle savedinstace) { super.oncreate(savedinstace); setcontentview(r.layout.main); create(); } }
you can't use things in ui thread background one. call:
iv.setimageresource(mthumbids[i]);
has done in main thread. in fact don't need background thread @ effect you're looking for. can make activity, no need implement runnable. , this:
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); iv = (imageview) findviewbyid(r.id.yourimageviewid); int = 0; runnable r = runnable(){ public void run(){ iv.setimageresource(mthumbids[i]); i++; if(i >= mthumbids.length){ = 0; } iv.postdelayed(r, 3000); //set go off again in 3 seconds. } }; iv.postdelayed(r,3000); // set first time 3 seconds
Comments
Post a Comment