javaandroidloopsimageviewthread-sleep

Controlling loop excecution speed in java - Android


I know that this is a very small question. But I can't figure out the reason for this. In my program I run a loop 50 times. Inside the loop I generate a random integer 0 to 5 and load an image(a dice image) to a imageView. So imageView should change rapidly and I should be able to see some visual like dice value is changing rapidly.

But I couldn't see any such change in imageView and at end of the loop, imageView changes at once.I mean if initial dice value is 2 it remains in two until loop finishes and then imageView suddenly change to 5 or some value like that.

I realized that I can't see a change in dice imageView since loop is executing very fast. Then after going through Loop execution speed control I put Thread.sleep method but still there is not a change.

This is my code:

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");
        }
    }

Can somebody explain the reason for this? I'm not getting any error in logcat.

this is 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

Solution

  • I solved the problem. The other answer also was very useful.

    I used following inside the run method.

    handler.postDelayed(this, 200);
    

    and it worked. This is the full answer, see 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);