在我的一个项目中,我正在从动态 url 加载图像。现在我需要显示加载对话框仍然加载所有图像。我正在使用异步任务加载图像。我是安卓新手。请任何人给我一点帮助
我的代码看起来像。
TableLayout table = (TableLayout)findViewById(R.id.tableLayout_1);
for(Integer i=0;i<2;i++){
TableRow rowP = new TableRow(this);
rowP.setBackgroundColor(Color.parseColor("#FFF000"));
ImageView image = new ImageView(this);
String ed="http://www.domain.com/image.jpg";
image.setTag(ed);
DownloadImagesTask td=new DownloadImagesTask();
td.execute(image);
rowP.addView(image);
table.addView(rowP);
}
}
private class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {
ImageView imageView = null;
@Override
protected Bitmap doInBackground(ImageView... imageViews) {
this.imageView = imageViews[0];
return download_Image((String)imageView.getTag());
}
@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
private Bitmap download_Image(String url) {
Bitmap bmp =null;
try{
URL ulrn = new URL(url);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
return bmp;
}catch(Exception e){}
return bmp;
}
}
提前致谢
编辑
private class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {
ImageView imageView = null;
ProgressDialog dialog;
Context context;
public DownloadImagesTask(Context context) {
this.context = context;
}
@Override
protected Bitmap doInBackground(ImageView... imageViews) {
this.imageView = imageViews[0];
return download_Image((String)imageView.getTag());
}
@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
if (dialog != null)
dialog.dismiss();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = ProgressDialog.show(context, "Title","Message");
}
private Bitmap download_Image(String url) {
Bitmap bmp =null;
try{
URL ulrn = new URL(url);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
return bmp;
}catch(Exception e){}
return bmp;
}
}