Android Imageswitcher:定期切换图像?

时间:2012-06-13 12:22:18

标签: android loops timer imageswitcher

我正在使用带有TouchListener的ImageSwitcher来更改数组中的图像。它工作正常,但我希望它每x秒左右切换一次图像,这样我就可以添加imageSwitcher.setImageResource(imageList[curIndex]);

有什么建议吗?

2 个答案:

答案 0 :(得分: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 :(得分: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.
                }