ProgressDialog显示不正确

时间:2016-11-26 09:54:57

标签: android progressdialog

我尝试在我的应用程序中实现progressDialog,这里是代码:

final ProgressDialog pd = new ProgressDialog(ctx);
pd.setTitle(R.string.creating_a_gif);
pd.setMessage(ctx.getString(R.string.preprocessing));
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
float duration = (float) (mModel.getGifEndPosition() - mModel.getGifStartPosition());
int totalFrames = (int) ((duration / SECOND_IN_MILLIS) * mModel.getFps());
pd.setMax(totalFrames);
pd.setIndeterminate(true);
pd.setCancelable(false);
pd.setCanceledOnTouchOutside(false);
pd.setButton(DialogInterface.BUTTON_NEGATIVE,
    ctx.getString(android.R.string.cancel),
    (dialogInterface, i) -> FFmpeg.getInstance(ctx).killRunningProcesses());
pd.show();

mBuilder.setProgressListener((f, ft) -> {
  pd.setMessage(ctx.getString(R.string.frames_processed));
  pd.setIndeterminate(false);
  new Thread(() -> {
    pd.setProgress(f);
    pd.setSecondaryProgress(f);
  }).start();
});
mBuilder.setCompleteListener(pathToGif -> {
  pd.dismiss();
  goToPreview(pathToGif);
});

当这个代码在一个应用程序会话期间第一次执行时,我的进度对话框会错过他的填充,如下所示: enter image description here

以及我的代码执行对话的所有下一次都没关系:

enter image description here

如何才能正确显示我的进度对话框?

1 个答案:

答案 0 :(得分:0)

更改为pd.setIndeterminate(false);

final ProgressDialog pd = new ProgressDialog(ctx);
pd.setTitle(R.string.creating_a_gif);
pd.setMessage(ctx.getString(R.string.preprocessing));
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
float duration = (float) (mModel.getGifEndPosition() - mModel.getGifStartPosition());
int totalFrames = (int) ((duration / SECOND_IN_MILLIS) * mModel.getFps());
pd.setMax(totalFrames);
pd.setIndeterminate(false); //<--- change to false
pd.setCancelable(false);
pd.setCanceledOnTouchOutside(false);
pd.setButton(DialogInterface.BUTTON_NEGATIVE,
    ctx.getString(android.R.string.cancel),
    (dialogInterface, i) -> FFmpeg.getInstance(ctx).killRunningProcesses());
pd.show();

mBuilder.setProgressListener((f, ft) -> {
  pd.setMessage(ctx.getString(R.string.frames_processed));
  pd.setIndeterminate(false);
  new Thread(() -> {
    pd.setProgress(f);
    pd.setSecondaryProgress(f);
  }).start();
});
mBuilder.setCompleteListener(pathToGif -> {
  pd.dismiss();
  goToPreview(pathToGif);
});