Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
图像视图在android中需要定期更改图像_Android_Image_Imageview - Fatal编程技术网

图像视图在android中需要定期更改图像

图像视图在android中需要定期更改图像,android,image,imageview,Android,Image,Imageview,我有一个图像视图。所以在图像视图中,我想在一段时间后更改图像。图像来自数组列表。现在,当图像数量为3个或多于3个时,它工作得非常完美。但是当它是2时,我的逻辑就不起作用了。第二个图像暂时可见,然后再次更改为第一个图像这是我的代码: r = new Runnable(){ int i = 0; public void run(){ iv.setImageBitmap(alBmps.get(i)); i++;

我有一个图像视图。所以在图像视图中,我想在一段时间后更改图像。图像来自数组列表。现在,当图像数量为3个或多于3个时,它工作得非常完美。但是当它是2时,我的逻辑就不起作用了。第二个图像暂时可见,然后再次更改为第一个图像这是我的代码:

r = new Runnable(){
        int i = 0;
        public void run(){
            iv.setImageBitmap(alBmps.get(i));
                i++;                
                if(i >= alBmps.size()){
                     i = 0;
                 }

             iv.postDelayed(r, 5000); //set to go off again in 5 seconds.
         }
    };
    iv.postDelayed(r, 1000);
有人能帮我修改一下上面的代码吗

谢谢。

试试这个

声明变量

static int i=0; 
private Timer myTimer;
在onCreate或on按钮中,单击要调用和启动方法的位置

myTimer = new Timer();

myTimer.schedule(new TimerTask() 
{           
    @Override
    public void run() 
    {   
        TimerMethod();
    }
}, 500, 5000);
将这些方法添加到类中

private void TimerMethod()
{
    this.runOnUiThread(Timer_Tick);
}

private Runnable Timer_Tick = new Runnable() 
{
    public void run()
    {
       if(i<alBmps.size())
       {
           iv.setImageBitmap(alBmps.get(i));
       }
       else
       {
           i=0;
           iv.setImageBitmap(alBmps.get(i));
       }   
       i++;
     }
};
private void TimerMethod()
{
此.runOnUiThread(计时器滴答声);
}
私有可运行计时器\u Tick=new Runnable()
{
公开募捐
{

如果(iSorry。这也不起作用。同样的问题也来了。你的第二张图像将在5秒钟内可见,你面临的确切问题是什么第二张图像在5秒钟内不可见。它来了一会儿,第一张图像再次显示。它来的就像一个闪光灯。请粘贴你的完整活动基本上是图像e从服务器下载,然后我显示。让我尝试使用drawable中的两个静态图像。