我目前正在练习带有数组列表的for循环。每次用户得到正确答案时,都会在序列中添加额外的随机值。因此,在开始时,一个图像闪烁,每次还有一个图像依次闪烁。
我遇到的问题是整个序列立即播放。例如,在第二次运行时,两个图像应该闪烁,一个闪烁,另一个闪烁。但我现在得到的是闪光。如何分解循环使一个闪烁然后另一个闪烁。
我的代码如下:
ArrayList<Integer> list = new ArrayList<Integer>();
int retval;
public void Run(){
Random rand = new Random();
int rndInt = rand.nextInt(5)+ 1 ;
list.add(rndInt);
int totalElements = list.size();
for(int i=0; i < totalElements;i++ ){
retval = list.get(i);
if (retval==1){
Centre.setImageResource(R.drawable.flash1);
Reset.start();// runs a class that resets the imageiew after a few seconds
}
else if (retval==2){
upperRight.setImageResource(R.drawable.flash2);
Reset.start();
}
else if (retval==3){
lowerRight.setImageResource(R.drawable.flash3);
Reset.start();
}
else if (retval==4){
lowerLeft.setImageResource(R.drawable.flash4);
Reset.start();
}
else if (retval==5){
upperLeft.setImageResource(R.drawable.flash5);
Reset.start();
}
}
}
修改
CountDownTimer Reset = new CountDownTimer(1000 , 0010){
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Centre.setImageResource(R.drawable.i1);
upperRight.setImageResource(R.drawable.i2);
lowerRight.setImageResource(R.drawable.i3);
lowerLeft.setImageResource(R.drawable.i4);
upperLeft.setImageResource(R.drawable.i5);
}
};
答案 0 :(得分:0)
好的,所以这里有修复,任何人都希望按顺序显示图像:
public void ZAP() {
if(i < totalElements){
retval =list.get(i);
if (retval==1){
check = 1;
Centre.setImageResource(R.drawable.flash1);
ResetTimer.start();
}
else if (retval==2){
check = 2;
upperRight.setImageResource(R.drawable.flash2);
ResetTimer.start();
}
else if (retval==3){
check =3;
lowerRight.setImageResource(R.drawable.flash3);
ResetTimer.start();
}
else if (retval==4){
check = 4;
lowerLeft.setImageResource(R.drawable.flash4);
ResetTimer.start();
}
else if (retval==5){
check=5;
upperLeft.setImageResource(R.drawable.flash5);
ResetTimer.start();
}}
}
public void Rand() {
i=0 ;
Random rand = new Random();
int rndInt = rand.nextInt(5)+ 1 ;
list.add(rndInt);
totalElements = list.size();
Log.d("LOOK", Integer.toString(rndInt));
Log.i("VALUE LIST ", list.toString()+" <<<<LIST HERE");
ZAP();