0

我有一个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();
}
4

3 回答 3

0

调用以下代码...

publishProgress(your time as integer);

eg:-publishProgress(100);

希望能帮助到你

于 2013-09-10T09:13:25.633 回答
0

我想出了以防万一有人需要这个。我将 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();
}
于 2013-09-10T09:32:43.550 回答
0

您可以自己手动更新它,使用您每 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
    }
}
于 2019-09-02T10:25:27.363 回答