有没有一种方法可以在每个需要更新ui组件的函数调用之间延迟某些秒数? [英] Is there an approach which can delay certain seconds between every function call which need to update ui components?

查看:82
本文介绍了有没有一种方法可以在每个需要更新ui组件的函数调用之间延迟某些秒数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个迷宫游戏,其功能之一是显示解决方案。该解决方案可能包含许多步骤,我使用一个for循环来迭代每个步骤,以便向玩家显示清楚,希望它可以在每个步骤之后暂停一秒钟。我知道延迟操作无法放在UI线程中,因此我尝试了几种异步延迟方法。不幸的是,由于其异步特性,如果解决方案有多个步骤,则UI不能更新,直到最后一步完成。

I am writing a maze game, and one of its features is "Show solution". The solution may contain many steps, I am using a for loop to iterate every step, in order to show it clear to a player, I wish it can pause one second around after every step. I know the delay operation cannot be put in UI thread, so I have tried several async delay approach. Unfortunately, because of its async nature, if the solution has more than one step, the UI cannot update till the last step finish. That's not I want certainly.

我尝试了 handler.postDelayed 方法, new Timer ().schedule 方法。

    public void showSolutionClick(View view) {
        int stage = currentGame.currentStage;
        int[][] solution = GameMap.solutionList[stage];
        drawStage(stage);
        for(int[] step: solution) {
            ImageView v = (ImageView)findViewById(viewIdList[step[0]][step[1]]);
            setTimeout(() -> {v.callOnClick();}, 1);
        }
    }

    private void setTimeout(Runnable r, int seconds) {
        Handler handler = new Handler();
        handler.postDelayed(r, seconds * 1000);
    }

也许我需要同步延迟方法,并且它不能阻止ui更新。

Maybe a synchronous delay approach is what I need, and it must not block the ui update.

更新:CountDownTimer适合这种情况。实际上,我在其他问题上也看到了CountDownTime答案,在我问之前我应该​​先尝试这种方法。谢谢所有给予答复的人。这是我的最终代码:

update: CountDownTimer is suitable for this situation. In fact, I have seen a CountDownTime answer at other questions, I should have tried that approach first before I ask. Thank you all who have given a response. Here's my final code:

    public void showSolutionClick(View view) {
        int stage = currentGame.currentStage;
        int[][] solution = GameMap.solutionList[stage];
        drawStage(stage);
        int stepTotal = solution.length;
        int stepPause = 1000;
        int timeTotal = (stepTotal + 1) * stepPause;

        new CountDownTimer(timeTotal, stepPause) {
            int stepIndex = 0;
            public void onTick(long timeRemain) {
                int[] pos = solution[stepIndex];
                ImageView v = (ImageView)findViewById(viewIdList[pos[0]][pos[1]]);
                v.callOnClick();
                stepIndex += 1;
                Log.d("time remain:", "" + timeRemain / 1000);
            }

            public void onFinish() {
                Log.d("final step", ":" + (stepIndex - 1));
            }
        }.start();
    }


推荐答案

您可以使用倒数计时器。

You can use a countdown timer.

 new CountDownTimer(30000, 1000){
                    public void onTick(long millisUntilFinished){
                        doUpdateOnEveryTick()
                    }
                    public  void onFinish(){
                       doActionOnFinish()
                    }
                }.start();

这篇关于有没有一种方法可以在每个需要更新ui组件的函数调用之间延迟某些秒数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆