我在应用程序中有一个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();
}
答案 0 :(得分:0)
调用以下代码......
publishProgress(your time as integer);
eg:-publishProgress(100);
希望有所帮助
答案 1 :(得分: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();
}
答案 2 :(得分:0)
您可以使用处理程序,每隔X ms对其进行更新,并设置您希望执行的频率。
另一种方法是使用类似的方法(以防不确定,并且您不介意使用旋转的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
}
}