我知道这是一个很小的问题。但我无法弄清楚这是什么原因。在我的程序中,我运行了一个循环 50 次。在循环内部,我生成一个 0 到 5 的随机整数并将图像(骰子图像)加载到 imageView。所以 imageView 应该会迅速变化,我应该能够看到一些视觉上的骰子值正在迅速变化。
但是我在 imageView 中看不到任何这样的变化,并且在循环结束时,imageView 立即更改。我的意思是,如果初始骰子值是 2,它会保持在两个中,直到循环完成,然后 imageView 突然变为 5 或类似的值.
我意识到我看不到 dice imageView 的变化,因为循环执行得非常快。然后经过 循环执行速度控制后,我放了 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 中没有收到任何错误。
这是日志猫:
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