簡體   English   中英

計算特定時間段內的數字,Java,Android

[英]Count up numbers in a specific time period, Java, Android

如何在 5 秒內在 TextView 中從0計數到number

number變量將始終具有不同的值,這就是為什么我需要它始終在 5 秒內從0計數到number

如果我做一段while並且number = 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();

試試這個方法:它應該適用於所有 > 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();

你為什么不試試這個:(適用於小於 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);
            }
        });
    }
}

})。開始();

下面是一些進行計數的代碼,但不使用任何 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數計算,所以一切都適用於小數(避免舍入錯誤)
  • 定義更新步驟(每次更新需要進行多遠)
  • Sleep for updateInterval - 更新 UI 所用的時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM