我知道这是一个非常小的问题。但我无法弄清楚原因。在我的程序中,我运行循环50次。在循环内部,我生成一个0到5的随机整数,并将图像(骰子图像)加载到imageView。所以imageView应该快速变化,我应该能够看到一些视觉上像骰子值正在快速变化。
但是我在imageView中看不到任何这样的变化,在循环结束时,imageView立即改变。我的意思是如果初始骰子值为2,它将保持为2,直到循环结束,然后imageView突然变为5或某些这样的价值。
我意识到我无法看到骰子imageView的变化,因为循环执行速度非常快。然后经过Loop execution speed control后我放了Thread.sleep方法,但仍然没有变化。
这是我的代码:
ImageView dice = (ImageView) findViewById(R.id.imageViewrollingdiceOne);
for (int i=0;i<50;i++){
int randomNum = random.nextInt(6);
System.out.println("Random Value"+randomNum);
dice.setImageResource(images[randomNum]);
try {
Thread.sleep(200);
} catch (Exception e) {
System.out.println("ex called");
}
}
有人可以解释一下这个原因吗?我在logcat中没有收到任何错误。
这是logcat:
02-05 18:38:38.515: D/OpenGLRenderer(27058): Flushing caches (mode 0)
02-05 18:38:42.179: D/TextLayoutCache(27455): Using debug level: 0 - Debug Enabled: 0
02-05 18:38:42.239: D/libEGL(27455): loaded /system/lib/egl/libGLES_android.so
02-05 18:38:42.239: D/libEGL(27455): loaded /system/lib/egl/libEGL_mali.so
02-05 18:38:42.249: D/libEGL(27455): loaded /system/lib/egl/libGLESv1_CM_mali.so
02-05 18:38:42.249: D/libEGL(27455): loaded /system/lib/egl/libGLESv2_mali.so
02-05 18:38:42.279: D/OpenGLRenderer(27455): Enabling debug mode 0
02-05 18:38:50.146: D/OpenGLRenderer(27455): Flushing caches (mode 0)
02-05 18:38:52.148: I/System.out(27455): Random Value0
02-05 18:38:52.348: I/System.out(27455): Random Value4
02-05 18:38:52.579: I/System.out(27455): Random Value2
02-05 18:38:52.789: I/System.out(27455): Random Value1
02-05 18:38:53.029: I/System.out(27455): Random Value2
02-05 18:38:53.219: I/System.out(27455): Random Value4
02-05 18:38:53.420: I/System.out(27455): Random Value1
02-05 18:38:53.620: I/System.out(27455): Random Value1
02-05 18:38:53.830: I/System.out(27455): Random Value5
02-05 18:38:53.870: D/dalvikvm(27455): GC_CONCURRENT freed 1035K, 11% free 9856K/10979K, paused 12ms+2ms
02-05 18:38:54.070: I/System.out(27455): Random Value1
02-05 18:38:54.300: I/System.out(27455): Random Value5
02-05 18:38:54.501: I/System.out(27455): Random Value1
02-05 18:38:54.701: I/System.out(27455): Random Value4
02-05 18:38:54.931: I/System.out(27455): Random Value1
02-05 18:38:55.131: I/System.out(27455): Random Value1
02-05 18:38:55.331: I/System.out(27455): Random Value1
02-05 18:38:55.532: I/System.out(27455): Random Value1
02-05 18:38:55.732: I/System.out(27455): Random Value1
02-05 18:38:55.942: I/System.out(27455): Random Value3
02-05 18:38:56.162: I/System.out(27455): Random Value1
02-05 18:38:56.362: I/System.out(27455): Random Value4
02-05 18:38:56.563: I/System.out(27455): Random Value5
02-05 18:38:56.763: I/System.out(27455): Random Value0
02-05 18:38:56.973: I/System.out(27455): Random Value2
02-05 18:38:57.193: I/System.out(27455): Random Value4
02-05 18:38:57.393: I/System.out(27455): Random Value3
02-05 18:38:57.594: I/System.out(27455): Random Value4
02-05 18:38:57.794: I/System.out(27455): Random Value4
02-05 18:38:58.004: I/System.out(27455): Random Value0
02-05 18:38:58.204: I/System.out(27455): Random Value0
02-05 18:38:58.404: I/System.out(27455): Random Value5
02-05 18:38:58.605: I/System.out(27455): Random Value0
02-05 18:38:58.805: I/System.out(27455): Random Value5
02-05 18:38:59.005: I/System.out(27455): Random Value0
02-05 18:38:59.215: I/System.out(27455): Random Value2
02-05 18:38:59.415: I/System.out(27455): Random Value5
02-05 18:38:59.616: I/System.out(27455): Random Value2
02-05 18:38:59.816: I/System.out(27455): Random Value5
02-05 18:39:00.016: I/System.out(27455): Random Value4
02-05 18:39:00.216: I/System.out(27455): Random Value3
02-05 18:39:00.416: I/System.out(27455): Random Value4
02-05 18:39:00.617: I/System.out(27455): Random Value4
02-05 18:39:00.817: I/System.out(27455): Random Value1
02-05 18:39:01.027: I/System.out(27455): Random Value5
02-05 18:39:01.227: I/System.out(27455): Random Value3
02-05 18:39:01.427: I/System.out(27455): Random Value3
02-05 18:39:01.628: I/System.out(27455): Random Value5
02-05 18:39:01.828: I/System.out(27455): Random Value2
02-05 18:39:02.028: I/System.out(27455): Random Value0
02-05 18:39:02.228: I/System.out(27455): Random Value2
答案 0 :(得分:3)
使用Thread.sleep
创建一个Thread或使用Handler for make wait来更改ImageView中的图像,而不是睡眠主UI线程:
new Handler().postDelayed(new Runnable() {
public void run() {
// put your code here to change image in ImageView...
}
}, 200);
答案 1 :(得分:1)
我解决了这个问题。以上答案也非常有用。
在run方法中使用以下内容。
handler.postDelayed(this, 200);
它有效。这是完整的答案。参考this。
final Handler handler = new Handler();
Handler.postDelayed(new Runnable(){
public void run(){
int randomNum = random.nextInt(6);
dice.setImageResource(images[randomNum]);
handler.postDelayed(this, 200);
}
}, 200);
感谢。