如何制作每30秒更新一次的图片



如何制作每30秒更新一次的图片?

ImageView imageView = (ImageView) findViewById(R.id.imageView);
Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(imageView);

您将要使用类似线程来执行此操作。

例如,在您的图像视图下方:

Runnable imageUpdater = new Runnable() {
    @Override
    public void run() {
        while (true) {
             try {
                 sleep(30000); // 30 seconds in milliseconds
            } catch(InterruptedException e) {
                 // Someone called thread.interrupt() and tried
                 // to stop the thread executing
                 return;
            }
            // Here load the image in the same way as above:
            // But you will need to go onto the UI thread first.
            image.post(new Runnable() {
                 public void run() {
                     Picasso.with(YourActivity.this).load( "http://i.imgur.com/DvpvklR.png").memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(imageView);
                 }
            });
         }
    }
};

然后,您只需启动可运行:

Handler handler = new Handler();
handler.post(imageUpdater);
    String[] imageLink = {http://i.imgur.com/DvpvklR.png, 
                              http://i.imgur.com/DvpvklR.png, http://i.imgur.com/DvpvklR.png};
    int position = 0;
    new Timer().scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(runnable);    
            }
        }, 30 * 1000, 30*1000);
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
           Picasso.with(this).load(imageLink[position%imageLink.length]).memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(imageView);
           position++;
         }
    };

希望上述代码能帮助您:)

相关内容

最新更新