开发者

Change images in real-time

开发者 https://www.devze.com 2023-02-15 06:10 出处:网络
I have this code: package com.example.helloandroid; import java.util.Random; import android.app.Activity;

I have this code:

package com.example.helloandroid;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;

public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        for(int i = 0; i < 6; 开发者_如何学编程i++) {
            TableLayout tl = (TableLayout) findViewById(R.id.T);
            TableRow tr = (TableRow) tl.getChildAt(i);
            for(int j = 0; j < 6; j++) {
                ImageView img = (ImageView) tr.getChildAt(j);
                Random randomGenerator = new Random();
                int randomInt = randomGenerator.nextInt(2);
                if (randomInt == 1) {
                    img.setImageResource(R.drawable.w);
                }
                else {
                    img.setImageResource(R.drawable.b);
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

I want that when I change an image, it change either on the screen. This code just freeze until all images are changed. Why?


Try using a AsynchTask for this, because all of your code runs on the main thread so it has to finish all the loops before the view is ready (explaining the freeze).


I have modified your code use try below one

public class HelloAndroid extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                for (int i = 0; i < 6; i++)
                {
                    TableLayout tl = (TableLayout) findViewById(R.id.T);
                    TableRow tr = (TableRow) tl.getChildAt(i);
                    for (int j = 0; j < 6; j++)
                    {
                        ImageView img = (ImageView) tr.getChildAt(j);
                        Random randomGenerator = new Random();
                        int randomInt = randomGenerator.nextInt(2);
                        runOnUiThread(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                if (randomInt == 1)
                                {
                                    img.setImageResource(R.drawable.w);
                                }
                                else
                                {
                                    img.setImageResource(R.drawable.b);
                                }
                            }
                        });
                        try
                        {
                            Thread.sleep(100);
                        }
                        catch (InterruptedException e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消