android - Thread.sleep导致应用程序滞后

时间:2014-06-02 15:32:52

标签: android thread-sleep

我想做app来显示3个元素(每2秒),每个元素都显示在before元素之后。这是代码:

public class Remember extends Activity
{
    TextView text;
    ImageView element1, element2, element3, element4 ,element5, element6;
    Button button1, button2, button3, button4, button5, button6;
    int data, id;
Random random;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_remember);

    random = new Random();

    id=1;

    text = (TextView)findViewById(R.id.text);

    element1 = (ImageView)findViewById(R.id.element1);
    element2 = (ImageView)findViewById(R.id.element2);
    element3 = (ImageView)findViewById(R.id.element3);
    element4 = (ImageView)findViewById(R.id.element4);
    element5 = (ImageView)findViewById(R.id.element5);
    element6 = (ImageView)findViewById(R.id.element6);

    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button3 = (Button) findViewById(R.id.button3);
    button4 = (Button) findViewById(R.id.button4);
    button5 = (Button) findViewById(R.id.button5);
    button6 = (Button) findViewById(R.id.button6);

    element1.setVisibility(View.GONE);
    element2.setVisibility(View.GONE);
    element3.setVisibility(View.GONE);
    element4.setVisibility(View.GONE);
    element5.setVisibility(View.GONE);
    element6.setVisibility(View.GONE);

    data = random.nextInt(6)+1;




}
public void button1(View v)
{
    gameStart();
}

public void gameStart()
{
    do{ 
    if (data==1)
    {
        CountDownTimer cdt = new CountDownTimer(2000, 1000)
        {

            @Override
            public void onTick(long millisUntilFinished)
            {
                element1.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFinish()
            {
                element1.setVisibility(View.GONE);

            }
        }.start();

    }
    else if (data==2)
    {
        CountDownTimer cdt = new CountDownTimer(2000, 1000)
        {

            @Override
            public void onTick(long millisUntilFinished)
            {
                element2.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFinish()
            {
                element2.setVisibility(View.GONE);

            }
        }.start();

    }
    else  if (data==3)
    {
        CountDownTimer cdt = new CountDownTimer(2000, 1000)
        {

            @Override
            public void onTick(long millisUntilFinished)
            {
                element3.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFinish()
            {
                element3.setVisibility(View.GONE);

            }
        }.start();

    }
    else if (data==4)
    {
        CountDownTimer cdt = new CountDownTimer(2000, 1000)
        {

            @Override
            public void onTick(long millisUntilFinished)
            {
                element4.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFinish()
            {
                element4.setVisibility(View.GONE);

            }
        }.start();

    }
    else if (data==5)
    {
        CountDownTimer cdt = new CountDownTimer(2000, 1000)
        {

            @Override
            public void onTick(long millisUntilFinished)
            {
                element5.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFinish()
            {
                element5.setVisibility(View.GONE);

            }
        }.start();

    }
    else if (data==6)
    {
        CountDownTimer cdt = new CountDownTimer(2000, 1000)
        {

            @Override
            public void onTick(long millisUntilFinished)
            {
                element6.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFinish()
            {
                element6.setVisibility(View.GONE);

            }
        }.start();

    }
    id=id+1;
    text.setText("cos " + id);
    data = random.nextInt(6)+1;
     try
    {
        Thread.sleep(2000);
    } catch (InterruptedException e)
    {
        e.printStackTrace();
    }
    }while(id<=3);
}

}

但是,如果我点击开始应用程序开始滞后,所有元素应该隐藏的所有元素都会变得规范。如果我删除Thread.sleep所有元素同时显示。我该怎么办?

1 个答案:

答案 0 :(得分:3)

ui线程上的

Thread.sleep(2000);阻塞了ui线程。你永远不应该阻止ui线程。取消睡眠。

要创建延迟,请使用Handler

http://developer.android.com/guide/components/processes-and-threads.html