Android:如何在循环中创建延迟,同时仍然更新 UI
这是我在这里发表的第一篇文章,如果我的问题格式不正确,我很抱歉。 我正在开发我的第一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将 Handler 与 Runnable (在其 run() 中执行循环的一次迭代)和
postDelayed(Runnable r, long延迟Millis)
延迟。计算迭代次数,以便您知道何时停止并使用最后一次removeCallbacks()
并在处理完成后调用一个方法来执行任何需要执行的操作。像
然后在 onCreate 或任何地方
}
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 toremoveCallbacks()
and call a method to do whatever needs to be once the dealing is done.Something like
And then in the onCreate or wherever
}
尝试
Thread.sleep(long millis)
。您必须将其放入 try/catch 块中。Try out
Thread.sleep(long millis)
. You'll have to put it in a try/catch block.如果上述解决方案对您不起作用,请尝试此处的 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
请在此处查看我的答案,它可能会对您有所帮助。不知道它是否正确,但它对我来说非常有效。
please check my answer here it may help you. dont know its proper way or not but it worked for me very fine.