java - 更新时间时进度条停止或滞后
问题描述
我在更新进度条时遇到了一个小问题。当用户按住录制按钮时,它会触发一个 CountDownTimer 任务,该任务会倒计时有多少秒的录制时间,并每 1000 毫秒增加已录制的秒数。当用户松开按钮时,它会取消 CountDownTimer 并在用户再次按下按钮后重新启动。有时当我按住记录按钮时,进度条会停止更新或滞后,即使我可以清楚地看到秒数仍在计数(我让它们在控制台上打印出来)。谁能帮我理解为什么它有时会滞后或停止?
这是任务:
countDownTimer = new CountDownTimer(millisLeftForRecording,1000) {
@Override
public void onTick(long l) {
millisLeftForRecording = l;
secondsRecorded++;
lastNumberOfSecondsRecorded++;
millisRecorded = secondsRecorded * 1000;
System.out.println("secondsRecorded "+ secondsRecorded);
}
@Override
public void onFinish() {
//Do something
}
};
这是触发录制的按钮
recordButton.setOnTouchListener((view, motionEvent) -> {
if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
stopRecording();
} else {
startRecording();
}
return true;
});
这是 startRecording 和 stopRecording 方法
private void startRecording(){
progressBarFill(countDownTimer,true, isRecording);
isRecording = true;
recordButton.setVisibility(View.INVISIBLE);
recordingImage.setVisibility(View.VISIBLE);
if(secondsRecorded >= 10){
saveVideoButton.setVisibility(View.VISIBLE);
} else {
saveVideoButton.setVisibility(View.INVISIBLE);
}
}
// Stop capturing video
private void stopRecording(){
progressBarFill(countDownTimer,false, isRecording);
isRecording = false;
videoLengthList.add(lastNumberOfSecondsRecorded);
System.out.println("Last number of seconds recorded: " + lastNumberOfSecondsRecorded);
lastNumberOfSecondsRecorded = 0;
System.out.println("Video length list size: " + videoLengthList.size());
videoSegmentsRecorded++;
System.out.println("\n\nVideo Segments Recorded " + videoSegmentsRecorded +"\n\n");
recordButton.setImageResource(R.drawable.record_button_unpressed_98);
recordButton.setVisibility(View.VISIBLE);
recordingImage.setVisibility(View.INVISIBLE);
if(videoSegmentsRecorded > 0){
deleteSegmentButton.setVisibility(View.VISIBLE);
}
}
虽然这会更新时间,但我有一种方法可以使用已过去的新秒数更新进度条。这是那个方法:
private void progressBarFill(CountDownTimer aCountDownTimer, boolean fillProgressBar, boolean alreadyRunning){
if(fillProgressBar && !alreadyRunning) {
aCountDownTimer.start();
System.out.println("\n\n\nCOUNT DOWN TIMER STARTED:\nAT " + secondsRecorded + " SECONDS\n\n\n");
} else if(!fillProgressBar && alreadyRunning){
aCountDownTimer.cancel();
System.out.println("\n\n\nCOUNT DOWN TIMER STOPPED:\nAT " + secondsRecorded + " SECONDS\n\n\n");
}
\\secondsRecorded is multiplied by 1000000 because the progress bar max is 60 multiplied by 1000000
videoLengthProgressBar.setProgress(secondsRecorded*1000000, true);
}
预先感谢您的帮助。
解决方案
推荐阅读
- flutter - Flutter Firestore 数据传输
- reactjs - 找不到模块:无法解析“react-bootstrap/Offcanvas”
- babylonjs - 在巴比伦js中沿轴旋转gltf模型
- kotlin - 常见的 KMM 依赖项Main
- flutter - 具有空安全性的 Flutter 表单验证
- python - 滚动背景中的 Pygame 故障
- flutter - 'Latest' 中列出的较新版本在颤振中可能无法相互兼容
- installation - 在“pip install jupyterlab”期间出现关于找不到文件的 OSError。我怎样才能解决这个问题?
- vue.js - 如何使拖放组件成为ul的一部分
- ios - 是否可以在 React Native 中从 iOS 应用程序打开日期和时间设置