android:关于闪烁屏幕的咨询

时间:2012-05-02 04:59:18

标签: android

我试着让我的屏幕像这样闪烁:

for (int i=0;i<10;i++)
        {
           targetView.setBackgroundColor(Color.BLUE);
           for (int j=0;j<500;j++); //delay
           targetView.setBackgroundColor(Color.RED);
        }

但它无法正常工作

2 个答案:

答案 0 :(得分:3)

试试这个,

targetView.setBackgroundColor(Color.BLUE);
try {
            Thread.sleep(5000); // milliseconds to wait...
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
targetView.setBackgroundColor(Color.RED);

修改

public class TSActivity extends Activity {


     LinearLayout lin;
     private int finalStatus = 0;
     private Handler colorHandler = new Handler();
     boolean flag = false;

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

        lin = (LinearLayout)findViewById(R.id.linMain);

            new Thread(new Runnable() {
                public void run() {
                    while (finalStatus < 100) {

                        // process some tasks
                        finalStatus = doSomeTasks();

                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        // Update the color
                        colorHandler.post(new Runnable() {
                            public void run() {
                                if(flag)
                                {
                                 lin.setBackgroundColor(Color.BLUE);
                                 flag = false;
                                }
                                else
                                {
                                    lin.setBackgroundColor(Color.GREEN);
                                     flag = true;
                                }
                            }
                        });
                    }

                }
            }).start();
    }

 // color changer simulator... a really simple
    public int doSomeTasks() {

        long fileSize = 0;

        while (fileSize <= 1000000) {

            fileSize++;

            if (fileSize == 100000) {
                return 10;
            } else if (fileSize == 200000) {
                return 20;
            } else if (fileSize == 300000) {
                return 30;
            }
            // ...add your own
            }

        return 100;
    }
}

希望这会对你有帮助..

...谢谢

答案 1 :(得分:0)

您也可以使用处理程序。

Runnable r=new Runnable(){
    public void run(){
        if(red){ 
            targetView.setBackgroundColor(Color.RED);
            red=false;
        }
        else{
            targetView.setBackgroundColor(Color.BLUE);
            red=true;
        }
        if(!stop_blinking) handler.postDelayed(this,1000);

    }
};

handler=new Handler();
handler.postDelayed(r,1000); //1000 is in mili secs.

stop_blinking是布尔值,只要您希望视图停止闪烁,就需要设置它。