如何分别为每个ImageView设置延迟?

问题描述 投票:0回答:2

我为我正在研究的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。我想在前一个图像资源之后设置一秒钟。我该如何实现这一目标?

android imageview delay
2个回答
0
投票

您可以将HandlerpostDelayed()方法用于延迟特定代码:

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

-1
投票

您可以尝试在行之间放置:

Thread.sleep(1000);

这将在每次更新图像视图后停止执行一秒钟。

© www.soinside.com 2019 - 2024. All rights reserved.