Android: changing Image with time interval

后端 未结 5 905
走了就别回头了
走了就别回头了 2020-12-05 21:20

I am using ImageDownloader class to get images from server and getting these images links using an ArrayList. After downloading the Image I am setting the Image

5条回答
  •  悲&欢浪女
    2020-12-05 22:08

    try this code.the images was saved in drawable. please do insert a imageview in xml code. noted that the time interval for the following code is 1 sec.

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

提交回复
热议问题