how to change images on imageView after some interval

后端 未结 5 1761
再見小時候
再見小時候 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: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) {
            }}};
    
    }
    

提交回复
热议问题