我需要让布局背景闪烁或闪烁,每秒都在变化。我写了这段代码:
int colours[]={0xff00ff00, 0xffff0000, 0xff0000ff,0xffffffff};
RelativeLayout fondo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fondo = (RelativeLayout) findViewById(R.id.fondo);
this.fondo.setBackgroundColor(colours[0]);
this.fondo.setOnTouchListener(this);
}
这是 onTouch 方法:
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
int i=1;
long delay= 1000;
long time = System.currentTimeMillis();
while(true) {
long time2 = System.currentTimeMillis();
long time3 = (time2 - time);
if (time3 > delay) {
this.fondo.setBackgroundColor(colours[i]);
Log.d("DEBUG", "EEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
time = System.currentTimeMillis();
i++;
if (i >= colores.length)
i=0;
}
}
}
return true;
}
我的问题是我无法每秒更改背景颜色,但我可以每秒编写 Log.d("DEBUG")。