在我的活动中,我下载了几个图像,然后通过在主布局中添加它们中的每一个来显示它们。我想这3种方法,但每一次我的进度对话框冻结,直到图像加载:对话框冻结异步任务android
1的方法:使用异步任务:我下载的doInBackground(图像),并把它们添加到主布局onPostExecute()
第二:与上面相同,但我加3的图像的每一个onProgressUpdate()
:使用一个处理器和一个线程
每次进度对话框冻结了几秒钟(实际上是整个应用程序冻结),直到图像下载并显示在屏幕上。
我一直在努力解决这个问题很长一段时间,但还没有。有任何想法吗?下面是我的代码的一部分:
protected Integer doInBackground(Void... params) {
//...
try {
//download images
}
} catch (JSONException e) {
e.printStackTrace();
}
if (!loadTask.isCancelled()) {
//Display images
int length=imageList.size();
for (i=0; i<length; i++) {
publishProgress(i);
}
}
return 1;
}
protected void onProgressUpdate(Integer... a) {
//interaction with UI thread
int i = a[0];
ImageView im = new ImageView (MyActivity.this);
im.setImageBitmap(getRemoteImage(imageUrl));
im.setLayoutParams(Params);
im.setAdjustViewBounds(true);
im.setPadding(px2, px2, px2, px2);
im.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//...
}
});
}
你是如何执行AsyncTask的? – dmon
loadTask = new LoadingTask(); loadTask.execute(); – MScott