开发者

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

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
        L开发者_运维技巧oadCard(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


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);

}


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


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


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();
                        }
              }
            } 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜