Android: how to create delay in loop while still updating the UI

拥有回忆 提交于 2019-12-08 06:02:21

问题


This is my first post here so sorry if I don't format my question correctly. I am developing my first Android app and it is a card game. I've developed the same card game in C# using visual studio. In C#, in order to simulate action and delay when dealing, etc, I slept the thread for a given amount of time, then called the method Application.DoEvents() which forced the UI to update. However, in java, I cannot seem to find an answer to this problem.

I'll post some code from my deal() method, which I need to delay so it looks like the cards are actually being dealt in a circle, and not all at once since the computer goes so fast:

private void dealHelper() { Hand pCards = Referee.getHumanHand();

    //show all cards in rotating order
    for(int i=0; i<CARD_COUNT; i++)
    {
        ///////////////////////////////////////////////////////////////////////
        // load each card into their appropriate ImageViews and make visible //
        ///////////////////////////////////////////////////////////////////////

        //human player
        LoadCard(playerCards.get(i), pCards.getCard(i));
        playerCards.get(i).setVisibility(View.VISIBLE);
        playerCards.get(i).setPadding(1, 1, 1, 1);

        // allow discarded cards to be clickable
        discardCardImages.get(i).setPadding(1, 1, 1, 1);

        //computer 1
        computer1Cards.get(i).setImageResource(R.drawable.cardskin);
        computer1Cards.get(i).setVisibility(View.VISIBLE);

        //computer 2
        computer2Cards.get(i).setImageResource(R.drawable.cardskin);
        computer2Cards.get(i).setVisibility(View.VISIBLE);

        //computer 3
        computer3Cards.get(i).setImageResource(R.drawable.cardskin);
        computer3Cards.get(i).setVisibility(View.VISIBLE);
    }
}

I need a slight delay of about 500ms between each card that is displayed on the screen to simulate action. I've searched and searched and haven't a solution that works (or I understand). Any help would be much appreciated.

Thank You, Daniel


回答1:


Use a Handler with a Runnable (which does one iteration of the loop in its run()) and postDelayed(Runnable r, long delayMillis) for the delay. Count the iterations so you know when to stop and use the last one to removeCallbacks() and call a method to do whatever needs to be once the dealing is done.


Something like

private final Handler mHandler = new Handler();
private int iLoopCount = 0;

private final Runnable rDealAndWait = new Runnable()
{
  public void run()
  {
    if (dealtAHand())
    {
      ++iLoopCount;
      mHandler.postAtTime(this, SystemClock.uptimeMillis() + DEAL_DELAY);
    }
    else
    {
      doAfterAllHandsDealt();
    }
  }
};


private boolean dealtAHand()
{
  if (i == CARD_COUNT) return false;

  //human player
  LoadCard(playerCards.get(iLoopCount), pCards.getCard(i));
  playerCards.get(iLoopCount).setVisibility(View.VISIBLE);
  playerCards.get(iLoopCount).setPadding(1, 1, 1, 1);

  // etc

  return true;
}

And then in the onCreate or wherever

  dealtAHand();
  mHandler.postAtTime(rDealAndWait, SystemClock.uptimeMillis() + DEAL_DELAY);

}




回答2:


Try out Thread.sleep(long millis). You'll have to put it in a try/catch block.




回答3:


If nothing works out for you from above solutions, then give a try to CountDownTimer here,

http://developer.android.com/reference/android/os/CountDownTimer.html




回答4:


please check my answer here it may help you. dont know its proper way or not but it worked for me very fine.

                @Override
                public void run() {
                    for(int i = 0;i<diceNum;i++){
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                            //do your Ui task here
                            }
                        });

                        try {

                            Thread.sleep(500);

                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
              }
            } 


来源:https://stackoverflow.com/questions/5996076/android-how-to-create-delay-in-loop-while-still-updating-the-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!