Android 异步加载 listview里的图片

如果listview里有图片,需要进行延迟加载,不然用户体验会非常差,卡在哪里不动。


解决办法:
在listview adapter的 设置 imgvidw的 方法里 写成如下, 最主要用到的是 AsyncTask
写一个 下载图片的内部类,getUrlimg是自己写的,根据自己的需求,该方法返回的是根据URL返回的 bitmap

private class ImageDownloadTask extends AsyncTask<Object, Object, Bitmap>{  
private ImageView imageView = null;
@Override
protected Bitmap doInBackground(Object... params) {
// TODO Auto-generated method stub
Bitmap bmp = null;
Log.d("tips","asdfasdfasdf" );
imageView = (ImageView) params[1];
try {
// bmp = BitmapFactory.decodeStream(new URL((String)params[0]).openStream());
bmp=getUrlimg((String)params[0]);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bmp;
}

protected void onPostExecute(Bitmap result){
imageView.setImageBitmap(result);
}
}


然后在 adapter里的设置 imageview的 时候调用这个方法 比如

private View makeItemView(String strTitle, String url) {
LayoutInflater inflater = (LayoutInflater) AddShowbuttonList.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// 使用View的对象itemView与R.layout.item关联
View itemView = inflater.inflate(R.layout.brandlistitem, null);

// 通过findViewById()方法实例R.layout.item内各组件
TextView title = (TextView) itemView.findViewById(R.id.branditemTitle);
title.setText(strTitle);
ImageView image = (ImageView) itemView.findViewById(R.id.branditemImage);
new ImageDownloadTask().execute(url, image);
// try {
// image.setImageBitmap(getUrlimg(url));
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//image.setImageResource(R.drawable.loading);
return itemView;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值