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.