列表视图上的异步图像加载器 [Android]

发布于 2024-09-07 21:00:39 字数 1328 浏览 2 评论 0原文

我在将异步图像加载器实现到以下代码时遇到问题。我在网上读了一些关于它的帖子,我想我理解它背后的逻辑,但我似乎未能实现它。

下面的代码是我用来简单地将图像加载到列表视图中的代码。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

顾挽 2024-09-14 21:00:39

您可以使用我的示例代码作为参考 ListView 中的图像延迟加载

You may use my sample code as reference Lazy load of images in ListView

十年九夏 2024-09-14 21:00:39

你看过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

苹果你个爱泡泡 2024-09-14 21:00:39

解决方案是在适配器中填充一个类变量,例如,一个引用所有“ImageView listDescription”的 ArrayList,

ArrayList<ImageView> allImageViews = new ArrayList<ImageView>();    
    ...

    public View getView(int position, View convertView, ViewGroup parent){
       ...
       ImageView listDescription=(ImageView)row.findViewById(R.id.listdescription);
       allImageViews.add(listDescription);
       ...
    }

    private class ImageDownLoader extends AsyncTask<ArrayList, Void, Void>{
       doInBackground(){
         for(ImageView imageView: allImageViews){
         BitmapFactory.Options bmOptions;
         bmOptions = new BitmapFactory.Options();
         bmOptions.inSampleSize = 1;
         bm = LoadImage(imageNameOrWhatever, bmOptions);
         imageView.setImageBitmap(bm);
       } 
    }

然后使用遍历每个 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"

ArrayList<ImageView> allImageViews = new ArrayList<ImageView>();    
    ...

    public View getView(int position, View convertView, ViewGroup parent){
       ...
       ImageView listDescription=(ImageView)row.findViewById(R.id.listdescription);
       allImageViews.add(listDescription);
       ...
    }

    private class ImageDownLoader extends AsyncTask<ArrayList, Void, Void>{
       doInBackground(){
         for(ImageView imageView: allImageViews){
         BitmapFactory.Options bmOptions;
         bmOptions = new BitmapFactory.Options();
         bmOptions.inSampleSize = 1;
         bm = LoadImage(imageNameOrWhatever, bmOptions);
         imageView.setImageBitmap(bm);
       } 
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文