如何分别为每个ImageView设置延迟?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何分别为每个ImageView设置延迟?相关的知识,希望对你有一定的参考价值。
我为我正在研究的Blackjack应用程序编写了以下方法。
public void showCard(View view){
startButton.setVisibility(View.INVISIBLE);
hitButton.setVisibility(View.VISIBLE);
standbutton.setVisibility(View.VISIBLE);
Card dealtCardOnePlayer = deck.dealACard();
Card dealtCardOneDealer = deck.dealACard();
Card dealtCardTwoPlayer = deck.dealACard();
Card dealtCardTwoDealer = deck.dealACard();
player.getCardsInHand().add(dealtCardOnePlayer);
dealer.getCardsInHand().add(dealtCardOneDealer);
player.getCardsInHand().add(dealtCardTwoPlayer);
dealer.getCardsInHand().add(dealtCardTwoDealer);
playerCardOne.setImageResource(dealtCardOnePlayer.getCardImagePath());
dealerCardOne.setImageResource(dealtCardOneDealer.getCardImagePath());
playerCardTwo.setImageResource(dealtCardTwoPlayer.getCardImagePath());
dealerCardTwo.setImageResource(R.drawable.back);
}
它基本上将前两张牌交给玩家和经销商。最后四行代码将图像资源设置为ImageViews。我想在前一个图像资源之后设置一秒钟。我该如何实现这一目标?
答案
您可以将Handler
的postDelayed()
方法用于延迟特定代码:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//execute code here after 1000 milliseconds
}
}, 1000);
静态:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
playerCardOne.setImageResource(dealtCardOnePlayer.getCardImagePath());
}
}, 1000); //1 second
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
playerCardOne.setImageResource(dealtCardOnePlayer.getCardImagePath());
}
}, 2000); //2 seconds
...and so on
另一答案
您可以尝试在行之间放置:
Thread.sleep(1000);
这将在每次更新图像视图后停止执行一秒钟。
以上是关于如何分别为每个ImageView设置延迟?的主要内容,如果未能解决你的问题,请参考以下文章
PyQt4:如何分别为QTabWidget中的每个选项卡着色?