ProgressDialog不会立即显示 [英] ProgressDialog doesn't appear immediately
问题描述
我有一个带有一些按钮的片段,当点击一个按钮时,它应该显示一个ProgressDialog,加载一个位图阵列并显示在画廊的片段中,关闭ProgressDialog。
但是ProgressDialog不会立即显示,它需要1到2秒的时间,并且在我的画廊展示时闪烁。
<点击后进行此操作:
try {
progress = ProgressDialog.show(activity,,载入图像,true);
//载入图片
//展示画廊
} catch(){
// ...
} finally {
handler.sendEmptyMessage(0);
我的处理器在onCreate:
handler = new Handler(){
public void handleMessage(Message msg){
progress.dismiss();
}
};
我使用Android 3.1
Logcat显示任何东西:(
03-09 13:17:32.310:D / DEBUG(5695):显示前()
03-09 13:17:32.350:D / DEBUG(5695):在show()之后
您正在加载主UI线程中的图像 - 您应该在后台进程中执行此操作,否则可能导致UI无响应(并导致您的 ProgressDialog
在错误的时间出现)。
你应该看看 AsyncTask 在后台加载图片。
显示 ProgressDialog
在 AsyncTask.onPreExecute
中,在中加载图片> AsyncTask.doInBackground
并关闭 AsyncTask.onPostExecute
中的对话框。
I have a fragment with some buttons in it, when a button is clicked it should show a ProgressDialog, load an array of bitmaps and show it in the fragment in a gallery, dismiss ProgressDialog.
But the ProgressDialog doesn't show immediately, it take something like 1 or 2 seconds and it just blink at the moment when my gallery is show.
Im doing this after click:
try{
progress = ProgressDialog.show(activity, "", "Loading images", true);
//load images
//show gallery
}catch(){
//...
}finally{
handler.sendEmptyMessage(0);
}
My Handler at onCreate:
handler = new Handler() {
public void handleMessage(Message msg) {
progress.dismiss();
}
};
Im using Android 3.1
Logcat shows anything :(
03-09 13:17:32.310: D/DEBUG(5695): before show()
03-09 13:17:32.350: D/DEBUG(5695): after show()
You are loading the images on the main UI thread - you should do this in a background process as it may cause your UI to become unresponsive (and cause your ProgressDialog
to show up at the wrong time).
You should look into using an AsyncTask to carry out loading of the images in the background.
Display the ProgressDialog
in AsyncTask.onPreExecute
, load images in AsyncTask.doInBackground
and dismiss the dialog in AsyncTask.onPostExecute
.
这篇关于ProgressDialog不会立即显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!