增加/减少SeekBar慢慢地

时间:2016-06-09 18:10:18

标签: java android

我试图用一个按钮让我的SeekBar缓慢增加&& ||递减到某个int值(也许我只能用双倍?),让我们说搜索栏的进度从0开始,我想在大约5秒的时间内增加到50。这在Android中可行吗?

 //  ON SEEKBAR CHANGE
   fill.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
            // TODO Auto-generated method stub

            fillProgress = String.valueOf(progress); 
            fillView.setText(fillProgress);

        }
    });

    centerDistribution.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            // TOAST: ARE YOU SURE YOU WANT TO fill bucket?
            // IF YES
            // INCREMENT SEEKBAR SLOWLY

    }
    });

1 个答案:

答案 0 :(得分:0)

我从未亲自将其用于此目的,但您可以使用AsyncTask确保您的用户界面在移动过程中保持响应。基本上,您使用AsyncTask就像更新进度条一样,但不是进度条,而是更新seekbar的值。 You'll need to override the entire class,但面包和黄油将在这里:

private class AsyncTaskSeekbar extends AsyncTask<String, String, String> 
{       
  @Override
  protected void doInBackground(String... params) 
  {
   try 
   {
    int time = 5000;           //5000 ms = 5 seconds  
    int incTime = 5000 / 50;   //we want it to get to 50 after 5 seconds 100ms per Seekbar unit

    for(int i = 0; i < time; i += incTime)
    {
        Thread.sleep(incTime);
        publishProgress(); // Calls onProgressUpdate()
    }
    } catch (InterruptedException e) {
    e.printStackTrace();
    } catch (Exception e) {
    e.printStackTrace();
   }
  }

  @Override
  protected void onProgressUpdate() 
  {
   seekBar.setProgess(seekBar.getProgess() + 1);
  }
}

当然,你必须做一些事情,比如初始化AsyncTaskSeekbar,动态传递值等等。但这应该会让你对如何入门有所了解。