【问题标题】:count-up effect for a textView in AndroidAndroid中textView的计数效果
【发布时间】:2015-05-11 10:01:59
【问题描述】:

我正在开发一个应用程序来计算段落/文本页面中的单词数。

扫描完成后,我希望在数字从 0 变为 TOTAL(字数)后显示输出的总字数。

例如:So, for 100 words: 0..wait..1..wait..2..wait..3..wait..4..wait..5,6,7,8,9 10.......99,100 and then STOP

我尝试了几种不同的技术:

 TextView sentScore = (TextView) findViewById(R.id.sentScore);

                long freezeTime = SystemClock.uptimeMillis();

                for (int i = 0; i < sent; i++) {
                    if ((SystemClock.uptimeMillis() - freezeTime) > 500) {
                        sentScore.setText(sent.toString());
                    }
                }

我也试过这个:

 for (int i = 0; i < sent; i++) { 
        // try {
            Thread.sleep(500);

        } catch (InterruptedException ie) {
            sentScore.setText(i.toString()); 
        } 
    }

但是没有任何帮助。我确信这些都是完全业余的尝试。

有什么帮助吗?谢谢

【问题讨论】:

    标签: android count textview


    【解决方案1】:

    以下代码将帮助您当前间隔为 100 毫秒,但您可以根据自己的方便进行更改

    for (int i = 0; i < sent; i++) {
            new Handler().postDelayed(new Runnable() {
    
            @Override
            public void run() {
                sentScore.setText(sent.toString());
            }
        }, 100 * i);
    }
    

    【讨论】:

    • 请记住,这段代码会产生大量增加内存消耗的处理程序。此外,没有办法中断后台工作。它不是一个专业的解决方案,这应该按照 Google 的建议使用 AsyncTask 来完成,如果活动暂停,它可以取消。它也没有按预期工作,因为所有处理程序都在 100 毫秒后执行,时间应该是 100*i 而不是 100。
    【解决方案2】:

    在 android 中你应该使用 AsyncTask 来完成这种工作

    http://developer.android.com/reference/android/os/AsyncTask.html

    private class CountUpTask extends AsyncTask<Void, Void, Void> {
    
        private TextView textview;
        private int current, total, interval;
    
        public CountUpTask(TextView textview, int total, int interval) {
            this.textview = textview;
            this.current = 0;
            this.total = total;
            this.interval = interval;
        }
    
        @Override
        protected void doInBackground() {
            while(this.current < this.total){
              Thread.sleep(this.interval);
              this.current++;
              publishProgress(this.current);
            }
    
            return null;
        }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            this.textview.setText("0");
        }
    
        @Override
        protected void onProgressUpdate(Integer... progress) {
            super.onProgressUpdate(progress);
            this.textview.setText(progress+"");
        }
    
        @Override
        protected void onPostExecute() {
    
        }
    }
    

    如您所见,doInBackground 在后台线程中执行,onPrexcute、onProgressUpdate 和 onPostExecute 在 UI 线程中执行,允许您更新 UI。

    CountUpTask countUpTask = new CountUpTask ((TextView) findViewById(R.id.sentScore), sent, 500);
    countUpTask.execute();
    

    【讨论】:

      猜你喜欢
      • 2017-01-22
      • 2012-04-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-22
      • 1970-01-01
      • 2016-08-27
      • 2016-05-24
      • 2016-12-02
      相关资源
      最近更新 更多