【问题标题】:Count up numbers in a specific time period, Java, Android统计特定时间段内的数字,Java,Android
【发布时间】:2015-12-23 11:44:01
【问题描述】:

如何在 TextView 中在 5 秒内从 0 计数到 number

number 变量总是有不同的值,这就是为什么我需要它总是在 5 秒内从0 计数到number

如果我执行 whilenumber = 1982654324441 将需要很长时间才能计算出来。

这就是我现在的做法:

new Thread(new Runnable() {
    public void run() {
        while (counter < number) {
            try {
                Thread.sleep(0,01);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            textView.post(new Runnable() {
                public void run() {
                    textView.setText("" + counter);
                }
            });
            counter++;
        }
    }
}).start();

【问题讨论】:

  • Thread.sleep(0,01); 应该做什么?使用 Thread.sleep(1000); 等待一秒钟,然后将您的 number 更改为 5。
  • 每次将新号码添加到 textView 时,我都等不及 1 秒。如果 number = 500 从 0 计数到 500 需要 500 秒...这是为了制作一个很好的计数动画。

标签: java android count textview


【解决方案1】:

试试这个方法:它应该适用于所有> 0的数字,你也可以调整更新间隔。

final int TIME_TO_COUNT = 5000; //ms
//Update interval in ms. Consider that the screen cannot be updated as often as you want.
//17ms (about 60FPS) sound reasonable
final int UPDATE_INTERVAL = 17;
final int number = 5001; //Can be any number between 0 and Integer.MAX_VALUE;

new Thread(new Runnable() {
    public void run() {
        double counter = 0.0;
        while (counter < number) {
            try {
                Thread.sleep(UPDATE_INTERVAL);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            textView.post(new Runnable() {
                public void run() {
                    textView.setText(String.valueOf(Math.ceil(counter)));
                }
            });
            counter += (number / (double) TIME_TO_COUNT) * UPDATE_INTERVAL;
        }
    }
}).start();

【讨论】:

  • 完美!真的很好用。我必须对其进行一些修改,因为我将处理非常大的数字!非常感谢!
【解决方案2】:

你为什么不试试这个: (适用于小于 5000 的数字,有轻微的舍入误差)

int stepTime = 5000/totalCount;
final CountDownTimer dialogTimer = new CountDownTimer(5000, stepTime) {

        public void onTick(long millisUntilFinished) {
              int num = (5000-millisUntilFinished) *stepTime;
              // use this num to set text
        }

        public void onFinish() {
        }
    };

编辑:

试试这个: (警告:未经测试的代码)

long startTime = System.currentTimeInMillis();
new Thread(new Runnable() {
public void run() {
    counter= number * (System.currentTimeInMillis() - startTime) /5000.0
    while (counter < number) {
        try {
            Thread.sleep(0,01);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        textView.post(new Runnable() {
            public void run() {
                textView.setText("" + counter);
            }
        });
    }
}

}).start();

【讨论】:

  • 这段代码看起来只适用于小于 5000 的数字。即便如此,我猜它也不是很可靠:您将在 stepTime 和 num 中出现舍入错误。
【解决方案3】:

这里有一些代码可以进行计数,但不使用任何 Android 功能。因此,您需要对其进行一些修改以满足您的需求。

public static void main(String[] args) throws Exception {
    countUpTo(5, 5);
    countUpTo(2, 7000);
}

private static void countUpTo(final int seconds, final int number) throws InterruptedException {
    int updateInterval = 100;
    int numUpdates = seconds * 1000 / updateInterval;
    double updateBy = number / (double) numUpdates;

    double count = 0;
    long startTime = System.currentTimeMillis();
    while(System.currentTimeMillis() - startTime < seconds*1000L) {
        long updateStartTime = System.currentTimeMillis();
        System.out.println((int) count);
        count += updateBy;
        long elapsedTime = System.currentTimeMillis() - updateStartTime;
        Thread.sleep(updateInterval-elapsedTime);
    }
    System.out.println(number);
}

这里的重要方面是:

  • 定义一个更新接口(在我的代码中每 100 毫秒)
  • 算上double,所以所有内容也适用于小数字(避免舍入错误)
  • 定义更新步骤(每次更新需要多长时间)
  • 为 updateInterval 休眠 - 更新 UI 所用的时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    相关资源
    最近更新 更多