how to change images on imageView after some interval

后端 未结 5 1744
再見小時候
再見小時候 2020-11-29 10:24

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

相关标签:
5条回答
  • 2020-11-29 11:05

    Try this..It works out well...

        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..
        }
    
    0 讨论(0)
  • 2020-11-29 11:10

    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.postDelayed(r,3000); // set first time for 3 seconds
    
    0 讨论(0)
  • 2020-11-29 11:18
    @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..
    
    }
    
    0 讨论(0)
  • 2020-11-29 11:23

    Try this it's working

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

    }

    0 讨论(0)
  • 2020-11-29 11:23

    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) {
            }}};
    
    }
    
    0 讨论(0)
提交回复
热议问题