如何在一定的时间间隔后更改ImageView的图像 [英] how to change images on imageView after some interval

查看:124
本文介绍了如何在一定的时间间隔后更改ImageView的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要粘贴的ImageView图像Android和该图片的一些间隔后定期更换的问题。意味着一个个的ImageView中显示的图像。我做这与线程的在Java的帮助,但我得到了一些问题,即线程不是附着有什么东西。请查看我的code下面给出告诉我确切的错误,以及如何删除错误或者给我一些不同势的方式这样做。

 包com.ex.thread;

进口com.ex.thread.R;

进口android.app.Activity;
进口android.os.Bundle;
进口android.util.Log;
进口android.widget.ImageView;

公共类线延伸活动实现Runnable {
/ **第一次创建活动时调用。 * /
公共静态整数[] mThumbIds = {

   R.drawable.al1,R.drawable.al2,R.drawable.al3,R.drawable.al4,

};
主题日;
ImageView的IV;
公共无效的run()
{
    的for(int i = 0;我3;;我++)
    {
        iv.setImageResource(mThumbIds [I]);
        的System.out.println(萨纳特潘迪);
        尝试{
            视频下载(3000);
        }赶上(例外五)
        {
            的System.out.println(E);
        }
    }
}
公共无效创建()
{
    主题日=新主题(新线程());
    th.start();
    尝试{
        视频下载(3000);
    }赶上(例外五)
    {
        的System.out.println(E);
    }
}

@覆盖
公共无效的onCreate(包savedInstace)
{
    super.onCreate(savedInstace);
    的setContentView(R.layout.main);
    创建();
}
}
 

解决方案

您不能使用的东西,在UI线程背景之一。所以,这个调用:

  iv.setImageResource(mThumbIds [I]);
 

必须在主线程中完成的。事实上,你也许并不需要一个后台线程都让你要找的效果。你可以说只是一个活动,没必要实现Runnable。然后做这样的事情:

  @覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    IV =(ImageView的)findViewById(R.id.yourImageViewID);
    INT I = 0;
    可运行R = Runnable接口(){
        公共无效的run(){
             iv.setImageResource(mThumbIds [I]);
             我++;
             如果(ⅰ> = mThumbIds.length){
                 I = 0;
             }
             iv.postDelayed(R,3000); //设置在3秒内熄灭了。
         }
    };
    iv.postDelated(R,3000); //设置第一次3秒
 

I have a problem that I want to paste images on ImageView in Android and that images are periodically changed after some interval. Means one by one images shown in ImageView. I am doing this with the help of Thread in java but I got some problem that Thread is not attached and something. Please review my code given below and tell me the exact error and how to remove that error or give me some diffrent way for doing this.

package com.ex.thread;

import com.ex.thread.R;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

public class thread extends Activity implements Runnable{
/** Called when the activity is first created. */
public static Integer[] mThumbIds = {

   R.drawable.al1,R.drawable.al2,R.drawable.al3,R.drawable.al4,

};
Thread th;
ImageView iv;
public void run()
{
    for(int i=0;i<3;i++)
    {
        iv.setImageResource(mThumbIds[i]);
        System.out.println("Sanat Pandey");
        try{
            Thread.sleep(3000);
        }catch(Exception e)
        {
            System.out.println(e);
        }
    }
}
public void create()
{
    Thread th = new Thread(new thread());
    th.start();
    try{
        Thread.sleep(3000);
    }catch(Exception e)
    {
        System.out.println(e);
    }
}

@Override
public void onCreate(Bundle savedInstace)
{
    super.onCreate(savedInstace);
    setContentView(R.layout.main);
    create();
}
}

解决方案

You can't use things in the UI thread from a background one. So this call:

iv.setImageResource(mThumbIds[i]);

Has to be done in the main thread. In fact you probably don't need a background thread at all to get the effect you're looking for. You can make that just an activity, no need to implement runnable. and then do something like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    iv = (ImageView) findViewById(R.id.yourImageViewID);
    int i = 0;
    Runnable r = Runnable(){
        public void run(){
             iv.setImageResource(mThumbIds[i]);
             i++;
             if(i >= mThumbIds.length){
                 i = 0;
             }
             iv.postDelayed(r, 3000); //set to go off again in 3 seconds.
         }
    };
    iv.postDelated(r,3000); // set first time for 3 seconds

这篇关于如何在一定的时间间隔后更改ImageView的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆