Changing image in imageview using Threads

前端 未结 6 1018
失恋的感觉
失恋的感觉 2020-12-11 10:05

I\'m getting error with this code. Why huhu 123123123

Thread timer = new Thread()
{
    public void run()
    {
        try
        {
            sleep(1500)         


        
6条回答
  •  失恋的感觉
    2020-12-11 10:42

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

提交回复
热议问题