How make images changes automatically using thread

Threads are not very favorable in case of android which is replace by AsyncTask .AsyncTask enables proper and easy use of the UI thread.This class allow to perform background operations and public results on the UT thread without having to manipulate threads or handlers.
Asyncask is designed to be helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTask should ideally be used for short operations( a few seconds at the most).

But still you change use threads in android but caution must be taken so as to stop unnecessary crashing of your application.

To change the images automatically just call the imageView

private Timer timer;
int[] images={R.id.1,R.id.2};
int count=0;

In the onCreateMethod use this

    timer=new Timer();
         timer.schedule(new TimerTask() {
   
    @Override
    public void run() {
     // TODO Auto-generated method stub
     runOnUiThread(new Runnable() {
     
      @Override
      public void run() {
       // TODO Auto-generated method stub
      if(count<=1)
      {
      img.setBackgroundResource(image[count]);
      count++;
      }else
      {
       count=0;
      img.setBackgroundResource(image[count]);
      }
      }
     });
    }
   }, 500,3000);
       
}

The 500,3000 are the times at which the thread runs.It can be changed according to your timings.



Comments

Popular posts from this blog

Making Bootable Pendrive with .bat file

Ek Beej Tha Gaya Bahut Hee Gaharaee Mein Boya - Nanha Paudha

R-W-W (Real, Win, Worth it ) - Boost your idea