I have an ordinary memory game. 12 squares and under them pictures I clicked on the square, the picture opened, then another square, if 2 pictures are the same, the squares remain open, and if not the same, after a short pause both squares are closed. The problem is that with unequal pictures no pause occurs. I tried Thread.sleep (500). Code below. With an even number of clicks, the number of attempts increases by 1. Equal-number of mutually equal patterns, if above 1, then the attempt is successful and both squares open without problems, and if not, then there is a problem: the second square does not open, and both squares are closing.

if (clicks % 2 == 0) { attempts++; for (int i = 0; i < 12; i++) { int equal = 0; for (int j = 0; j < 12; j++) { if (imageViews[i].getDrawable().getConstantState().equals(imageViews[j].getDrawable().getConstantState())) equal++; } if (equal > 1) { imageViews[i].setImageDrawable(imageViews[i].getDrawable()); } else { try { Thread.sleep(500); } catch (InterruptedException e) { } imageViews[i].setImageDrawable(imageViews[i].getDrawable()); imageViews[i].setImageDrawable(getResources().getDrawable(R.drawable.qm)); } } } opened = 0; 

How to make visible and "wrong" picture?

    1 answer 1

    In your case, Thread.sleep(500); hangs UI and therefore you do not see the picture. Well, the rest. Bring processing to a separate thread / use Handler / AsyncTask . For example:

     if (clicks % 2 == 0) { attempts++; for (int i = 0; i < 12; i++) { int equal = 0; for (int j = 0; j < 12; j++) { if (imageViews[i].getDrawable().getConstantState().equals(imageViews[j].getDrawable().getConstantState())) equal++; } if (equal > 1) { imageViews[i].setImageDrawable(imageViews[i].getDrawable()); } else { final Handler handler = new Handler(); //Потом правильно сделать Handler handler.postDelayed(new Runnable() { @Override public void run() { imageViews[i].setImageDrawable(getResources().getDrawable(R.drawable.qm)); } }, 500); imageViews[i].setImageDrawable(imageViews[i].getDrawable()); } } } opened = 0;