Android: changing Image with time interval

后端 未结 5 910
走了就别回头了
走了就别回头了 2020-12-05 21:20

I am using ImageDownloader class to get images from server and getting these images links using an ArrayList. After downloading the Image I am setting the Image

5条回答
  •  渐次进展
    2020-12-05 21:55

    Set up your ImageView like this:

      
    

    You can Use TimerTask to achieve this.

    // Declaration and Initialization :

    List mImageUrl = new ArrayList();
    private ImageLoader mImageLoader =  new ImageLoader(MainActivity.this);
    Timer timer = new Timer(); // changed
    int i = 0;
    

    // Put this code in your onCreate :

    timer.scheduleAtFixedRate(new TimerTask() {
    
                @Override
                public void run() {
                    if (i < mImageUrl.size()) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                mImageLoader.DisplayImage(mImageUrl.get(i), img);
                                i++;
                            }
                        });
                    } else {
                        i = 0;
                    }
                }
            }, 0, 2000);
    

    The timer task will start in 0 seconds and it will change the background every 2 seconds. You can change this as like as you want. Its working fine. I have tested.

    You can read more about the timertask here.

    You can also cancel the timer with the help of timer.cancel() HIH.

提交回复
热议问题