【问题标题】:how to set image in imageview after a definite interval?如何在一定的时间间隔后在 imageview 中设置图像?
【发布时间】:2012-08-16 11:36:00
【问题描述】:

我有一个图像视图和一个包含图像 URL 的数组。我必须每 3 秒遍历一次数组并在图像视图中设置图像...比如在图像视图中的起始图像,其 url 位于数组的索引零处然后在 3 秒后图像视图应该在数组的索引 1 处显示图像,依此类推。请帮助

【问题讨论】:

    标签: android


    【解决方案1】:

    使用它来定期更新您的图像视图...

     Timer timer = null;
     int i = 0;
    
            imgView=(ImageView)findViewById(R.id.img);
                timer = new Timer("TweetCollectorTimer");
                        timer.schedule(updateTask, 6000L, 3000L);//here 6000L is starting //delay and 3000L is periodic delay after starting delay
    
    
                private TimerTask updateTask = new TimerTask() {
    
                        @Override
                        public void run() {
    
                            YourActivity.this.runOnUiThread(new Runnable() {
    
                                @Override
                                public void run() { // TODO Auto-generated method stub
                                    imgView.setImageResource(photoAry[i]);
                                    i++;
                                    if (i > 5)
                                    {
                                        i = 0;
                                                        }
                                }
    
                            });
                        }
                    };
                int photoAry[] = { R.drawable.photo1, R.drawable.photo2, R.drawable.photo3,
                            R.drawable.photo4, R.drawable.photo5, R.drawable.photo6 };
    

    为了阻止这种情况,您可以致电

    timer.cancel();
    

    【讨论】:

      【解决方案2】:

      尝试使用处理程序并在处理程序代码中将图像设置为 imageView。

      【讨论】:

      【解决方案3】:

      您应该为此使用Handler's postDelayed 函数。它将以指定的延迟on the main UI thread 运行您的代码,因此您将能够update UI controls

      private int mInterval = 3000; // 3 seconds by default, can be changed later
        private Handler mHandler;
      
        @Override
        protected void onCreate(Bundle bundle) {
          ...
          mHandler = new Handler();
        }
      
        Runnable mStatusChecker = new Runnable() {
          @Override 
          public void run() {
            updateYourImageView(); //do whatever you want to do in this fuction.
            mHandler.postDelayed(mStatusChecker, mInterval);
          }
        };
      
        void startRepeatingTask() {
          mStatusChecker.run(); 
        }
      
        void stopRepeatingTask() {
          mHandler.removeCallbacks(mStatusChecker);
        }
      

      【讨论】:

        【解决方案4】:

        您可以使用特定时间段的计时器,在时间间隔后重复该功能。

        你可以使用如下代码:

        ImageView img = (ImageView)findViewById(R.id.imageView1);
        int delay = 0; // delay for 0 milliseconds.
        int period = 25000; // repeat every 25 seconds.
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new SampleTimerTask(), delay, period);
        
        public class SampleTimerTask extends TimerTask {
            @Override
            public void run() {
                //MAKE YOUR LOGIC TO SET IMAGE TO IMAGEVIEW
                img.setImageResource(R.drawable.ANYRANDOM);
            }
        }
        

        希望它对你有用。

        【讨论】:

        • 是的,不要忘记最后使用“timer.cancel();”取消计时器
        猜你喜欢
        • 2018-01-18
        • 2011-07-07
        • 2011-05-23
        • 1970-01-01
        • 2010-09-20
        • 2017-09-06
        • 1970-01-01
        • 2020-06-21
        • 2013-08-08
        相关资源
        最近更新 更多