如何在一段时间后更改imageView上的图像

时间:2011-03-02 12:37:10

标签: android

我有一个问题,我想在Android上的ImageView上粘贴图像,并且在一段时间后定期更改图像。表示ImageView中显示的逐个图像。我在Java的帮助下这样做但是我遇到了一些问题,即Thread没有附加什么。请查看下面给出的代码并告诉我确切的错误以及如何删除该错误或为我提供一些不同的方法。

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

6 个答案:

答案 0 :(得分:31)

试试这个..效果很好......

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);``
//
//
int []imageArray={R.drawable.img1,R.drawable.img2,R.drawable.img3};


final Handler handler = new Handler();
         Runnable runnable = new Runnable() {
            int i=0;
            public void run() {
                imageView.setImageResource(imageArray[i]);
                i++;
                if(i>imageArray.length-1)
                {
                i=0;    
                }
                handler.postDelayed(this, 50);  //for interval...
            }
        };
        handler.postDelayed(runnable, 2000); //for initial delay..
    }

答案 1 :(得分:13)

您无法在后台UI线程中使用UI线程。所以这个电话:

iv.setImageResource(mThumbIds[i]);

必须在主线程中完成。事实上,您可能根本不需要后台线程来获得您正在寻找的效果。你可以只做一个活动,不需要实现runnable。然后做这样的事情:

@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.postDelayed(r,3000); // set first time for 3 seconds

答案 2 :(得分:0)

尝试使用与TimerTask相关联的处理程序

答案 3 :(得分:0)

试试这个 它的工作

public class vv extends Activity {     int b [] = {R.drawable.a,R.drawable.m,R.drawable.b,R.drawable.j,R.drawable.er,R.drawable.chan,R.drawable.vv};     public ImageView i;     int z = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    i = (ImageView) findViewById(R.id.image);
    i.setImageResource(b[0]);
    Thread timer = new Thread() {
        public void run() {
            try {
                sleep(2000);

                for (z = 0; z < b.length + 2; z++) {
                    if (z < b.length) {
                        sleep(2000);
                        runOnUiThread(new Runnable() {
                            public void run() {
                                i.setImageResource(b[z]);
                            }
                        });
                    } else {
                        z = 0;

                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                System.out.println("finally");
            }
        }
    };
    timer.start();
}

}

答案 4 :(得分:0)

尝试此代码。图像保存在drawable中。请在xml代码中插入imageview。注意到以下代码的时间间隔是1秒。

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
public ImageView iv;
public static Integer[] mThumbIds = {
        R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4};
int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iv = (ImageView) findViewById(R.id.imageView);
    i=0;
    t.start();
}
Thread t = new Thread() {
    @Override
    public void run() {
        try {
            while (!isInterrupted()) {
                Thread.sleep(1000);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        iv.setImageResource(mThumbIds[i]);
                        i++;
                        if(i >= mThumbIds.length){
                            i = 0;
                        }}});}} 
        catch (InterruptedException e) {
        }}};

}

答案 5 :(得分:-1)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final int img[] = {R.drawable.flower1, R.drawable.flower2, R.drawable.flower3, R.drawable.flower4};

    layout = (RelativeLayout) findViewById(R.id.activity_main);

    final Handler handler=new Handler();

        Runnable runnable = new Runnable() {
           int i = 0;

            @Override
            public void run() {

                layout.setBackgroundResource(img[i]);
                i++;
                if (i > img.length - 1) {
                i = 0;
                }
                handler.postDelayed(this, 4000);  //for interval 4s..

            }
        };handler.postDelayed(runnable, 100); //for initial delay..

}