Android:如何在循环中创建延迟,同时仍然更新 UI

发布于 2024-11-06 18:22:28 字数 1575 浏览 0 评论 0原文

这是我在这里发表的第一篇文章,如果我的问题格式不正确,我很抱歉。 我正在开发我的第一个 Android 应用程序,它是一款纸牌游戏。我使用 Visual Studio 在 C# 中开发了相同的纸牌游戏。在 C# 中,为了模拟处理时的操作和延迟等,我让线程休眠给定的时间,然后调用方法 Application.DoEvents() 强制 UI 更新。然而,在java中,我似乎无法找到这个问题的答案。

我将发布来自 deal() 方法的一些代码,我需要延迟这些代码,以便看起来卡片实际上是在循环中发的,而不是一次全部发的,因为计算机运行得如此之快:

private void dealHelper() { 手牌 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);
    }
}

我需要在屏幕上显示的每张卡片之间有大约 500 毫秒的轻微延迟来模拟动作。我搜索了又搜索,但没有有效的解决方案(或者我理解)。任何帮助将不胜感激。

谢谢你, 丹尼尔

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静若繁花 2024-11-13 18:22:28

HandlerRunnable (在其 run() 中执行循环的一次迭代)和 postDelayed(Runnable r, long延迟Millis) 延迟。计算迭代次数,以便您知道何时停止并使用最后一次 removeCallbacks() 并在处理完成后调用一个方法来执行任何需要执行的操作。


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

然后在 onCreate 或任何地方

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

}

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

}

风轻花落早 2024-11-13 18:22:28

尝试Thread.sleep(long millis)。您必须将其放入 try/catch 块中。

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

时光礼记 2024-11-13 18:22:28

如果上述解决方案对您不起作用,请尝试此处的 CountDownTimer,

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

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

故事↓在人 2024-11-13 18:22:28

请在此处查看我的答案,它可能会对您有所帮助。不知道它是否正确,但它对我来说非常有效。

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

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();
                        }
              }
            } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文