【问题标题】:How to make a progressBar update faster in Android?如何在 Android 中更快地更新进度条?
【发布时间】:2013-09-10 08:57:36
【问题描述】:

我在一个应用程序中有一个ProgressBar,它需要一整秒才能更新,但我很确定它所在的线程设置为每 100 毫秒更新一次。我希望ProgressBar 更新得更快,可能在 100 毫秒级别或更平滑。

相关代码:

/**
 * Update timer on seekbar
 * */
public void updateProgressBar() {
    mHandler.postDelayed(mUpdateTimeTask, 100);
}   

/**
 * Background Runnable thread
 * */
private Runnable mUpdateTimeTask = new Runnable() {

       public void run() {
           long totalDuration = mPlayer.getDuration();
           long currentDuration = mPlayer.getCurrentPosition();

           // Displaying Total Duration time
           soundTotalDurationTextView.setText(""+utils.milliSecondsToTimer(totalDuration));
           // Displaying time completed playing
           soundCurrentDurationTextView.setText(""+utils.milliSecondsToTimer(currentDuration));

           // Updating progress bar
           int progress = utils.getProgressPercentage(currentDuration, totalDuration);
           soundProgressBar.setProgress(progress);

           // Running this thread after 100 milliseconds
           mHandler.postDelayed(this, 100);
       }
};

/**
 * Function to get Progress percentage
 * @param currentDuration
 * @param totalDuration
 * */
public int getProgressPercentage(long currentDuration, long totalDuration){
    Double percentage = (double) 0;

    long currentSeconds = (int) (currentDuration / 1000);
    long totalSeconds = (int) (totalDuration / 1000);

    // calculating percentage
    percentage =(((double)currentSeconds)/totalSeconds)*100;

    // return percentage
    return percentage.intValue();
}

【问题讨论】:

    标签: java android eclipse handler android-progressbar


    【解决方案1】:

    我想出了以防万一有人需要这个。我将 currentSeconds 和 totalSeconds 的除数从 1000 更改为 100。这给了我一个更大(更精确)的数字。

     /**
     * Function to get Progress percentage
     * @param currentDuration
     * @param totalDuration
     * */
    public int getProgressPercentage(long currentDuration, long totalDuration){
        Double percentage = (double) 0;
    
        long currentSeconds = (int) (currentDuration / 100);
        long totalSeconds = (int) (totalDuration / 100);
    
        // calculating percentage
        percentage =(((double)currentSeconds)/totalSeconds)*100;
    
        // return percentage
        return percentage.intValue();
    }
    

    【讨论】:

      【解决方案2】:

      调用以下代码...

      publishProgress(your time as integer);
      
      eg:-publishProgress(100);
      

      希望对你有帮助

      【讨论】:

        【解决方案3】:

        您可以自己手动更新它,使用您每 X 毫秒更新一次的处理程序,设置您希望执行的频率。

        另一种方法是使用类似的方法(以防它不确定并且您不介意使用旋转的 ImageView),这样可以加速:

        class MainActivity : AppCompatActivity() {
            private val handler = Handler()
            var curRotationIncrease = INITIAL_ROTATION_INCREASE
            var rotationIncreasePerFrame = INITIAL_ROTATION_INCREASE_PER_FRAME
        
            override fun onCreate(savedInstanceState: Bundle?) {
                super.onCreate(savedInstanceState)
                setContentView(R.layout.activity_main)
                curRotationIncrease = INITIAL_ROTATION_INCREASE
                rotationIncreasePerFrame = INITIAL_ROTATION_INCREASE_PER_FRAME
                val mSearchAnimationRunnable = object : Runnable {
                    override fun run() {
                        spinningImageView.rotation = spinningImageView.rotation + curRotationIncrease
                        if (curRotationIncrease + rotationIncreasePerFrame < MAX_ROTATION_INCREASE)
                            curRotationIncrease += rotationIncreasePerFrame
                        if (rotationIncreasePerFrame + ROTATION_INCREASE_INCREASE_PER_FRAME < MAX_ROTATION_INCREASE_PER_FRAME)
                            rotationIncreasePerFrame += ROTATION_INCREASE_INCREASE_PER_FRAME
                        handler.postDelayed(this, FRAME_STEP_IN_MS)
                    }
                }
                mSearchAnimationRunnable.run()
            }
        
            companion object {
                private const val INITIAL_ROTATION_INCREASE = 5f
                private const val INITIAL_ROTATION_INCREASE_PER_FRAME = 0.1f
                private const val TARGET_FPS = 60L
                private const val FRAME_STEP_IN_MS = 1000L / TARGET_FPS
                private const val ROTATION_INCREASE_INCREASE_PER_FRAME = 1f
                private const val MAX_ROTATION_INCREASE_PER_FRAME = 2f
                private const val MAX_ROTATION_INCREASE = 50f
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-01-24
          • 1970-01-01
          • 2012-03-06
          • 2016-08-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多