计算特定时间段内的数字,Java,Android

时间:2015-12-23 11:44:01

标签: java android count textview

如何在5秒内将TextView从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();

3 个答案:

答案 0 :(得分:2)

尝试此方法:它适用于所有数字&gt; 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();

答案 1 :(得分:0)

为什么不试试这个: (适用于小于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);
            }
        });
    }
}

})开始();

答案 2 :(得分:0)

以下是一些进行计数的代码,但不使用任何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);
}

这里的重要方面是:

  • 定义更新interfal(在我的代码中每100 ms)
  • 计入double所以一切都适用于小数字(避免舍入错误)
  • 定义更新步骤(每次更新需要多长时间)
  • Sleep for updateInterval - 更新UI所花费的时间。