我试着让我的屏幕像这样闪烁:
for (int i=0;i<10;i++)
{
targetView.setBackgroundColor(Color.BLUE);
for (int j=0;j<500;j++); //delay
targetView.setBackgroundColor(Color.RED);
}
但它不会工作
我试着让我的屏幕像这样闪烁:
for (int i=0;i<10;i++)
{
targetView.setBackgroundColor(Color.BLUE);
for (int j=0;j<500;j++); //delay
targetView.setBackgroundColor(Color.RED);
}
但它不会工作
尝试这个,
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;
}
}
希望对你有帮助..
谢谢...
您也可以使用处理程序。
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 是布尔值,当您希望视图停止闪烁时需要设置它。