1

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

4

4 回答 4

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();
于 2012-08-21T06:50:41.857 回答
0

您应该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);
  }
于 2014-12-05T10:40:43.580 回答
0

try using a handler and set the image to the imageView in the handler code.

于 2012-08-21T06:44:13.407 回答
0

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

您可以使用如下代码:

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

希望它对你有用。

于 2012-08-21T06:51:06.053 回答