我将在前面找到答案,并在这里找到类似的问题,但是没有找到一个适用于我的案例。 :(
我正在尝试在Eclipse中创建一个* Simon Says * esque Android游戏,我创建了一种选择随机颜色的方法(添加到以前选择的颜色的arraylist)。该阵列看起来像{“蓝色”,“绿色”,“黄色”}等。一旦添加了颜色,我希望能够遍历表格并“闪烁”相应的按钮以表示这是正确的回答。例如,如果所选颜色为蓝色,绿色和黄色,则会执行以下操作:
闪蓝色
闪烁绿色
闪烁黄色
//“闪烁”只会改变ImageButton的背景
然而,我希望能够“延迟”即将到来的颜色的闪烁,因为毫不拖延,我无法分辨闪烁的是什么,因为所有这些都会瞬间闪现。反正有没有做我正在描述的事情?我已经尝试过thread.Sleep(),我认为这不是我想要的,因为它冻结了屏幕。我也听说过Runnables和Timers,但我不知道这是否适用于这种情况或如何实现它。
我正在使用方法changeImage()来“闪现”颜色。类似的东西:
public void chooseColor() {
//add color to arraylist
for (String currentColor : colorarraylist) {
changeImage();
//delay
changeImage();
}
}
我一直在努力解决这个问题,任何帮助都会受到赞赏。
完整方法:
public void chooseColor() {
chooseOn = false;
//Adds random color to answerList
String randomColor = colorChoices[(int)(Math.random()*5)];
answerList.add(randomColor);
Log.v("", "ChosenColor: " + randomColor);
for (String Answer : answerList) {
ImageButton selectedButton;
Log.v("", "Answer: " + Answer);
playSound(R.raw.beep1);
//Set selectedButton to current color in answerList
int buttonID = getResources().getIdentifier(Answer.toLowerCase() + "_Button", "id", getPackageName());
selectedButton = (ImageButton) findViewById(buttonID);
//Set selectedButton's color (background) to look highlighted
changeImage(Answer.toLowerCase() + "light", selectedButton);
changeImage(Answer.toLowerCase(), selectedButton);
}
chooseOn = true;
}
答案 0 :(得分:2)
您可以将逻辑放在线程中并在循环的迭代之间休眠。在更改图像时,您需要创建一个可运行的UI线程。
如果我正确理解你的完整方法,那么这样的事情应该有效:
private int colorFlashTime = 500;
private int colorOffTime = 100;
....
public void chooseColor() {
//Adds random color to answerList
String randomColor = colorChoices[(int)(Math.random()*5)];
answerList.add(randomColor);
Log.v("", "ChosenColor: " + randomColor);
Thread colorThread = new Thread() {
public void run() {
chooseOn = false;
for (final String Answer : answerList) {
Runnable changeImageRunnableLight = new Runnable() {
public void run() {
ImageButton selectedButton;
Log.v("", "Answer: " + Answer);
playSound(R.raw.beep1);
//Set selectedButton to current color in answerList
int buttonID = getResources().getIdentifier(Answer.toLowerCase() + "_Button", "id", getPackageName());
selectedButton = (ImageButton) findViewById(buttonID);
//Set selectedButton's color (background) to look highlighted
changeImage(Answer.toLowerCase() + "light", selectedButton);
}
};
runOnUiThread(changeImageRunnableLight);
try {
Thread.sleep(colorFlashTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
Runnable changeImageRunnable = new Runnable() {
public void run() {
changeImage(Answer.toLowerCase(), selectedButton);
}
};
runOnUiThread(changeImageRunnable);
try {
Thread.sleep(colorOffTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
chooseOn = true;
}
};
colorThread.start();
}
答案 1 :(得分:2)
试试这段代码......希望有所帮助
Thread timer = new Thread(){
@Override
public void run() {
try {
sleep(1000);//time in milliseconds
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
//Do something after timer runs out or call some functions
}
super.run();
}
};
timer.start();