2012-06-13 61 views
4

我正在使用帶有TouchListener的ImageSwitcher來更改陣列中的圖像。它的工作正常,但我希望它每隔x秒左右切換圖像,以便我可以將imageSwitcher.setImageResource(imageList[curIndex]);添加到它。Android Imageswitcher:定期切換圖像?

有什麼建議嗎?

回答

16

試試這個,

imageSwitcher.postDelayed(new Runnable() { 
      int i = 0; 
      public void run() { 
       imageSwitcher.setImageResource(
        i++ % 2 == 0 ? 
         R.drawable.image1 : 
         R.drawable.mage2); 
       imageSwitcher.postDelayed(this, 1000); 
      } 
     }, 1000); 
+1

優秀。乾杯! –

+0

沒問題...總是歡迎.. –

4

我認爲可以通過TimerTask和Timer。請嘗試此代碼。我認爲它可以幫助你。

private Handler mHandler; 
    private Runnable mUpdateResults; 
     private Timer timerAnimate; 
     private TimerTask timerTask; 
     mHandler = new Handler(); 
     mUpdateResults = new Runnable() { 
      public void run() { 
       AnimateandSlideShow(); 
      } 
     }; 

     int delay = 0; 
     int period = 15000; 
     timerAnimate = new Timer(); 
     timerTask = new TimerTask() { 
      public void run() { 
       mHandler.post(mUpdateResults); 
      } 
     }; 
     timerAnimate.scheduleAtFixedRate(timerTask, delay, period); 

     Public void AnimateandSlideShow() 
       { 
       imageSwitcher.setImageResource(imageList[curIndex]); 
       ///Here You need To handle curIndex position. 
       }