Search code examples
javaandroidfor-loopthread-sleep

Changing images in a Loop - Android


I'm wondering why still I couldn't to figure out a way to do this. Although it seems like very simple, I spent my entire day for this. But couldn't do that.

I have set of dice images. 1.png,2.png,.... and 6.png. There is an ImageView in my layout. That is,

ImageView dice = (ImageView) findViewById(R.id.imageViewrollingdiceOne);

Here I want to change this imageView rapidly to see a some kind of a visual/animation using above 6 images. For that I wrote following piece of code.

Code 1:

for (int j=0;j<10;j++){
        int randomNum = random.nextInt(6);
            System.out.println("Random Value " + randomNum);
            dice.setImageResource(images[randomNum]);               
    }

Output:

There is not a visual. imageView remains unchanged and suddenly changes at the loop last iteration. I thought that it is because the loop is executing very fast. Then I did the following.

Code 2:

for (int j=0;j<10;j++){
        int randomNum = random.nextInt(6);
            System.out.println("Random Value " + randomNum);
            dice.setImageResource(images[randomNum]);
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }               
    }

Output:

Again there is not a visual. imageView remains unchanged and suddenly changes at the loop last iteration. Then I did the following.

Code 3:

final Handler localHandler = new Handler();
    Runnable runnableObject = new Runnable() {
        public void run() {
            final ImageView dice = (ImageView) findViewById(R.id.imageViewrollingdiceOne);
            int randomNum = random.nextInt(6);
            System.out.println("Random Value" + randomNum);
            dice.setImageResource(images[randomNum]);
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    };
    for (int j=0;j<10;j++){
        localHandler.postDelayed(runnableObject, 1000);
    }

Again there is not a visual. imageView remains unchanged and suddenly changes at the loop last iteration. There are no any errors in logcat in all three cases.

I found that threading also doesn't do the trick.


Solution

  • First of all there is already an animation set in android that can help you achive what you are after it is called FrameAnimation, here is an example on how to use it:

    FrameAnimation Example

    Your First, second and third code are running in the main thread, you should never use sleep in the main thread !.

    If you still want to set the image resource manually you can use this code:

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                int randomNum = random.nextInt(6);
                dice.setImageResource(images[randomNum]);
                handler.postDelayed(this, 500);
            }
        }, 500);