0

我正在构建一个会闪烁屏幕的应用程序,我需要不断更改我的 android 应用程序背景颜色使用

设置背景颜色

我得到了基本的背景颜色更改,但是当我实现以下代码时,应用程序崩溃了

void testthread()
{
    new Thread( new Runnable(){
        @Override
        public void run(){
            Looper.prepare();
            //do work here
            while(true)
            {
            background_White();
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            background_Black();
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            background_White();
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            background_Black();
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }



        }
    }).start();

}

函数 background_White 和 background_Black

    public void background_White() {

    View flashScreen = findViewById(R.id.flashScreen);
    flashScreen.setBackgroundColor(Color.WHITE);

}

public void background_Black() {

    View flashScreen = findViewById(R.id.flashScreen);
    flashScreen.setBackgroundColor(Color.BLACK);

你们能否为我提供有关如何使我的计划发挥作用的见解?

4

1 回答 1

1

我不确定,但不是线程尝试使用Handler -Runnable 作为,

 handler.postDelayed(runnable, 1);  

private Runnable runnable = new Runnable() {
public void run() {  
    runOnUiThread(new Runnable() { 
        public void run() 
        { 
            //*change background here**/                       
        } 
    }); 

我认为您的应用程序出现问题是因为另一个线程更新了您的 UI,因此请尝试使用上述 Handler-Runnable。

于 2012-12-17T05:18:08.380 回答