【问题标题】:Android ProgressBar: loading with TimerAndroid ProgressBar:使用计时器加载
【发布时间】:2011-12-06 08:44:52
【问题描述】:

更新: 这篇文章是我在用android 2.2学习android的时候写的,一定要检查它是否与你正在使用的api级别兼容。

为如何使用计时器加载进度条做了很多工作,我尝试使用已发布的方法,但总是会出现指针异常,或者测试应用程序会在启动时崩溃。

我的问题是,大声笑有没有人跑过任何关于如何做到这一点的教程或示例?我在想我需要的只是一个while循环,但还没有看到在计时器上完成。我是一个完全的菜鸟,但正在缓慢但肯定地学习

我的计时器

final Timer t = new Timer();
    t.schedule(new TimerTask() {
        public void run() {
            d.dismiss();  //MY DIALOG WINDOW
            t.cancel();
        }
    }, 7000);   

这是我试图开始工作的一个,但我认为我对它造成的伤害比它好

isRunning = false;
    // handler for the background updating
    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            total = total - 1;
            String perc = String.valueOf(total).toString();
            a.setText(perc + " SECONDS  until close");
            bar.incrementProgressBy(25);
        }
    };

    super.onStart();
    // reset the bar to the default value of 0
    bar.setProgress(0);
    // create a thread for updating the progress bar
    Thread background = new Thread(new Runnable() {
        public void run() {
            try {
                for (int i = 4; i < 20 && isRunning; i++) {
                    // wait 1000ms between each update
                    Thread.sleep(1000);
                    handler.sendMessage(handler.obtainMessage());
                }
            } catch (Throwable t) {
            }
        }
    });
    isRunning = true;
    // start the background thread
    background.start();

希望有人能够为我阐明这一点,一如既往地为简短的通用问题感到抱歉,只是想了解如何使用计时器加载进度条

再次感谢所有帮助

【问题讨论】:

    标签: android android-2.2-froyo


    【解决方案1】:

    使用android.os.CountDownTimer

     countDownTimer = new CountDownTimer(length_in_milliseconds,period_in_milliseconds) {
            private boolean warned = false;
            @Override
            public void onTick(long millisUntilFinished_) {
               progressBar.progress = (length_in_milliseconds-millisUntilFinished_)/lenght_in_milliseconds*100.0;
            }
    
            @Override
            public void onFinish() {
                // do whatever when the bar is full
            }
        }.start();
    

    【讨论】:

    • 为我节省了很多时间!谢谢
    • 这段代码运行良好,但应用程序进入后台进入前台如何更新进度条?它显示不同的时间
    【解决方案2】:

    here我的回答:

    您可以使用ObjectAnimator 为ProgressBar 的进度设置动画:

    ObjectAnimator animation = ObjectAnimator.ofInt(pb, "progress", 0, 100);
    animation.setDuration(5000);
    animation.setInterpolator(new DecelerateInterpolator());
    animation.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animator) { }
    
        @Override
        public void onAnimationEnd(Animator animator) {
            //do something when the countdown is complete
        }
    
        @Override
        public void onAnimationCancel(Animator animator) { }
    
        @Override
        public void onAnimationRepeat(Animator animator) { }
    });
    animation.start();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多