反复更改应用程序背景颜色:Android

时间:2012-12-17 04:57:26

标签: android

我正在构建一个会使屏幕闪烁的应用, 我需要不断使用

更改我的Android应用程序背景颜色
  

setBckgroundColour

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

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

你们能告诉我如何让我的计划有效吗?

1 个答案:

答案 0 :(得分:1)

我不确定但不是线程尝试使用处理程序 -Runnable as,

 handler.postDelayed(runnable, 1);  

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

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

相关问题