1

I'm making a game in Android that has 1 ImageView and 4 images and buttons. I set up the ImageView so that a random image is placed in it and a new random image is displayed after so much time has passed. I need to find a way to put a delay between the changing of the images that doesn't lock the UI. Here is the code that is relevant.

public class MainGameActivity extends Activity {

    int points = 0, score = 0, timer = 1000;
    ImageView imgView = (ImageView)findViewById(R.id.image);
    Random randy = new Random();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_game_activity);
        while(points >= 0) {
            changeImage();
        //Timer goes here
        }       
    }

    private void changeImage() {
        int randyInt = randy.nextInt(4);
        if (randyInt == 0){
            imgView.setImageDrawable(getResources().getDrawable(R.drawable.cheese));
        } else if (randyInt == 1) {
            imgView.setImageDrawable(getResources().getDrawable(R.drawable.worm1));
        } else if (randyInt == 2) {
            imgView.setImageDrawable(getResources().getDrawable(R.drawable.worm2));
        } else if (randyInt == 3) {
            imgView.setImageDrawable(getResources().getDrawable(R.drawable.minnow));
        }
    }
}

The 4 buttons use the same drawables as the ImageView. When the button is pressed the drawable in the ImageView and the drawable in the Image Button are checked for equivalence, so the UI still has to work while the while loop containing the pause in changing the ImageView drawable is still running. Thanks for any assistance you can give.

Giacomoni
  • 1,468
  • 13
  • 18
James
  • 7
  • 1

3 Answers3

0

Sounds like you need a timer. That has been asked and answered. Please have a look at this stackoverflow answer Android timer? How-to?

Community
  • 1
  • 1
Lasse
  • 1,153
  • 1
  • 10
  • 21
0

also you may try a Switch case instead of all those ifs

Switch(randyInt){

case 1:imgView.setImageDrawable(getResources().getDrawable(R.drawable.cheese));
break;
case 2:imgView.setImageDrawable(getResources().getDrawable(R.drawable.worm1));
break;
case 3:imgView.setImageDrawable(getResources().getDrawable(R.drawable.worm2));
break;
case 4:imgView.setImageDrawable(getResources().getDrawable(R.drawable.minnow));
break;
default:imgView.setImageDrawable(getResources().getDrawable(R.drawable.cheese));
break;
}
Technivorous
  • 1,682
  • 2
  • 16
  • 22
0

You can use the Timer like Lasse pointed in his answer. First of all you need to declare it:

private CountDownTimer timer;

Then in your onCreate you can call the timer (In this example, I'm changing the image every 5 seconds (5000ms), but you can change it if you want):

timer = new CountDownTimer(5000, 20) {

    @Override
    public void onTick(long millisUntilFinished) {

    }

    @Override
    public void onFinish() {
        try{
            callImage();
        }catch(Exception e){
        }
    }
};

Then in your callImage method, you set the image on the imageView and start the timer again:

private void callImage(){
    int randyInt = randy.nextInt(4);
    if (randyInt == 0){
        imgView.setImageDrawable(getResources().getDrawable(R.drawable.cheese));
    } else if (randyInt == 1) {
        imgView.setImageDrawable(getResources().getDrawable(R.drawable.worm1));
    } else if (randyInt == 2) {
        imgView.setImageDrawable(getResources().getDrawable(R.drawable.worm2));
    } else if (randyInt == 3) {
        imgView.setImageDrawable(getResources().getDrawable(R.drawable.minnow));
    }
    try{
        imgView.post(new Runnable() {

            @Override
            public void run() {
                timer.start();
            }
        });
    }catch(Exception e){
        Log.e("Error", "Error: " + e.toString());
    }

}

So, every 5 seconds (or the time that you need) it will change the image, and will not block the UI.

Hope it helps!

Community
  • 1
  • 1
Giacomoni
  • 1,468
  • 13
  • 18