列表视图上的异步图像加载器 [Android]
我在将异步图像加载器实现到以下代码时遇到问题。我在网上读了一些关于它的帖子,我想我理解它背后的逻辑,但我似乎未能实现它。
下面的代码是我用来简单地将图像加载到列表视图中的代码。
public class MyCustomAdapter extends ArrayAdapter<RSSItem> {
Bitmap bm;
public MyCustomAdapter(Context context, int textViewResourceId, List<RSSItem> list) {
super(context, textViewResourceId, list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
bm = LoadImage(myRssFeed.getList().get(position).getDescription(), bmOptions);
View row = convertView;
if(row == null) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.rsslist, parent, false);
}
TextView listTitle = (TextView)row.findViewById(R.id.listtitle);
listTitle.setText(myRssFeed.getList().get(position).getTitle());
ImageView listDescription = (ImageView)row.findViewById(R.id.listdescription);
listDescription.setImageBitmap(bm);
TextView listPubdate = (TextView)row.findViewById(R.id.listpubdate);
listPubdate.setText(myRssFeed.getList().get(position).getPubdate());
return row;
}
}
I'm having problems implementing an asynchronous image loader to the following code. I read some posts around the web about it and I think I understand the logic behind it, but I seem to fail in implementing it.
The code bellow is what I use to simply load the images in my listview.
public class MyCustomAdapter extends ArrayAdapter<RSSItem> {
Bitmap bm;
public MyCustomAdapter(Context context, int textViewResourceId, List<RSSItem> list) {
super(context, textViewResourceId, list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
bm = LoadImage(myRssFeed.getList().get(position).getDescription(), bmOptions);
View row = convertView;
if(row == null) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.rsslist, parent, false);
}
TextView listTitle = (TextView)row.findViewById(R.id.listtitle);
listTitle.setText(myRssFeed.getList().get(position).getTitle());
ImageView listDescription = (ImageView)row.findViewById(R.id.listdescription);
listDescription.setImageBitmap(bm);
TextView listPubdate = (TextView)row.findViewById(R.id.listpubdate);
listPubdate.setText(myRssFeed.getList().get(position).getPubdate());
return row;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用我的示例代码作为参考 ListView 中的图像延迟加载
You may use my sample code as reference Lazy load of images in ListView
你看过SmartImageView吗?
http://loopj.com/android-smart-image-view/
非常好异步加载图像的简单库(:
该库的一些功能
ImageView 的直接替代品
从 URL 加载图像
从手机联系人通讯录加载图像
图片异步加载,加载发生在UI线程之外
图像缓存到内存和磁盘以实现超快速加载
SmartImage 类可以轻松扩展以从其他源加载
Have you looked SmartImageView?
http://loopj.com/android-smart-image-view/
It's very simple library to load images asynchronously (:
some Features of this library
Drop-in replacement for ImageView
Load images from a URL
Load images from the phone’s contact address book
Asynchronous loading of images, loading happens outside the UI thread
Images are cached to memory and to disk for super fast loading
SmartImage class is easily extendable to load from other sources
解决方案是在适配器中填充一个类变量,例如,一个引用所有“ImageView listDescription”的 ArrayList,
然后使用遍历每个 ImageView 的 AsyncTask,检索关联的 Image 并从 ArrayList 中删除 ImageView。它会在后台一次下载一个,而您的图形用户界面仍然会响应。
On solution would be to populate a class variable within your adapter, say, an ArrayList with the references all the "ImageView listDescription"
Then use an AsyncTask that goes through each ImageView, retrieves the associated Image and removes the ImageView from the ArrayList. It will download one at a time in the background while your gui will still respond.