Android Imageswitcher: switch images periodically?

▼魔方 西西 提交于 2019-12-05 01:15:58

问题


I am using an ImageSwitcher with a TouchListener to change images from an array. Its working fine but i want it to switch images every x seconds or so, so that I can add imageSwitcher.setImageResource(imageList[curIndex]); to it.

Any suggestions?


回答1:


Try this,

 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);



回答2:


I think it is possible via TimerTask and Timer. please Try this code. I think It help you.

    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.
                }


来源:https://stackoverflow.com/questions/11014729/android-imageswitcher-switch-images-periodically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!