我有一个ProgressDialog,它是在创建Activity时启动的(insideOnCreate方法)。
private void initDialog() {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setIndeterminate(true);
mProgressDialog.setTitle("Please wait.");
mProgressDialog.setMessage("Connecting to LinkedIn.");
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
2500ms后我想将圆形动画更改为我的自定义图像(见下文)(就像我完成从服务器检索一些数据并完成时的模拟一样。)所以我需要向用户显示该过程已完成。为了这个目标,我选择了下一个方式。
这我有一个问题。当我第一次设置一个drawable
private void initDialog() {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setIndeterminate(true);
mProgressDialog.setTitle("Please wait.");
mProgressDialog.setMessage("Connecting to LinkedIn.");
mProgressDialog.setCancelable(false);
//This
mProgressDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.ok));
mProgressDialog.show();
}
ProgressDialog更改drawable,所有看起来都没问题。但是当ProgressBar启动(工作/循环动画播放)时,我尝试重置一个可绘制的
mProgressDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.ok));
我有意想不到的结果:圆形动画消失,没有图像设置为当前进度可绘制。没有图像! BLANK AREA而不是我的形象。
authButton.postDelayed(new Runnable() {
@Override
public void run() {
mProgressDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.ok)); //doesn't work as expected!
}
}, 2500);
答案 0 :(得分:5)
ProgressDialog
包含ProgressBar
内显示我们的drawable。
View view = inflater.inflate(a.getResourceId(com.android.internal.R.styleable.AlertDialog_horizontalProgressLayout, R.layout.alert_dialog_progress), null);
mProgress = (ProgressBar) view.findViewById(R.id.progress); // THIS
mProgressNumber = (TextView) view.findViewById(R.id.progress_number);
mProgressPercent = (TextView) view.findViewById(R.id.progress_percent);
setView(view);
当我们尝试再次将drawable设置为ProgressDialog
时,ProgressDialog 确实设置了一个新的drawable 。 但是这个drawable没有边界(显示此视图的坐标)。
所以我们必须设置它。我选择了一种复制方式 - 只是从当前可绘制的ProgressDialog
开始的复制边界。你可以用另一种方式。
private void initDialog() {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setIndeterminate(true);
mProgressDialog.setTitle("Please wait.");
mProgressDialog.setMessage("Connecting to LinkedIn.");
mProgressDialog.setCancelable(false);
}
mProgressDialog.show();
private void changeToDone(int resId) {
//Getting a progressBar from dialog
ProgressBar bar = (ProgressBar) mProgressDialog.findViewById(android.R.id.progress);
//Getting a DONE(new) drawable from resources
Drawable drawable = getResources().getDrawable(resId);
//Getting a drawable from progress dialog
Drawable indeterminateDrawable = bar.getIndeterminateDrawable();
//Obtain a bounds of current drawable
Rect bounds = indeterminateDrawable.getBounds();
//Set bounds to DONE(new) drawable
drawable.setBounds(bounds);
//Set a new drawable
bar.setIndeterminateDrawable(drawable);
mProgressDialog.setTitle("Done.");
mProgressDialog.setMessage("Connected.");
}
在
等特定情况下未经过测试的解决方案ActionBar
/ TitleBar
android.R.progress
的另一种情况。在这种情况下,我认为我的解决方案会产生意想不到的行为。答案 1 :(得分:2)
private void initDialog(boolean is) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setIndeterminate(true);
mProgressDialog.setTitle("Please wait.");
mProgressDialog.setMessage("Connecting to LinkedIn.");
mProgressDialog.setCancelable(false);
if(is) // take global variable
mProgressDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.ic_launcher));
mProgressDialog.show();
if(!is)
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mProgressDialog.cancel();
mProgressDialog = null;
initDialog(true);
}
}, 2500);
首次使用initDialog(false);
据我检查不确定的drawable无法真正替换,因此这种解决方法可以帮助您避免创建自定义ProgressBar。