• Android之使用Volley框架在ListView中加载大量图片


    1.listview 中的条目要用 Volley 中的 NetworkImageView,如果直接用ImageView也可以,但是要在getView方法中使用url地址设置为imageView的tag标记

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/userimage"
            android:layout_width="100dp"
            android:layout_height="100dp" />
    
    </LinearLayout>

    2.新建图片缓存类 BitmapCache

    import android.graphics.Bitmap;
    import android.support.v4.util.LruCache;
    
    import com.android.volley.toolbox.ImageLoader.ImageCache;
    
    public class BitmapCache implements ImageCache {
        private LruCache<String, Bitmap> mCache;
    
        public BitmapCache() {
            int maxSize = 4 * 1024 * 1024;
            mCache = new LruCache<String, Bitmap>(maxSize) {
                @Override
                protected int sizeOf(String key, Bitmap bitmap) {
                    //                return bitmap.getByteCount(); // >api12 才能用
                    return bitmap.getRowBytes() * bitmap.getHeight();
                }
            };
        }
    
        @Override
        public Bitmap getBitmap(String url) {
            return mCache.get(url);
        }
    
        @Override
        public void putBitmap(String url, Bitmap bitmap) {
            if (bitmap != null) {
                mCache.put(url, bitmap);
            }
        }
    }

    3.adapater代码

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    
    import com.android.volley.RequestQueue;
    import com.android.volley.toolbox.ImageLoader;
    import com.android.volley.toolbox.NetworkImageView;
    import com.android.volley.toolbox.Volley;
    
    public class MyAdapter extends BaseAdapter {
        private String[] list;
        private Context context;
        private RequestQueue queue;
        private ImageLoader imageLoader;
    
        public MyAdapter(Context context, String[] list) {
            this.context = context;
            this.list = list;
            queue = Volley.newRequestQueue(context);
            imageLoader = new ImageLoader(queue, new BitmapCache());
        }
    
        @Override
        public int getCount() {
            return list.length;
        }
    
        @Override
        public Object getItem(int position) {
            return list[position];
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
    
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null);
                holder.img = (NetworkImageView) convertView.findViewById(R.id.userimage);
    
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
    
            final String imgUrl = list[position];
    
            if (imgUrl != null && !imgUrl.equals("")) {
                holder.img.setDefaultImageResId(R.drawable.ic_launcher);
                holder.img.setErrorImageResId(R.drawable.ic_launcher);
                holder.img.setImageUrl(imgUrl, imageLoader);
            }
    
            return convertView;
        }
    
        class ViewHolder {
            NetworkImageView img;
        }
    }
  • 相关阅读:
    【算法】百度百科经典算法链接集
    【剑指offer】38.字符串的排列
    Spring整合RabbitMQ
    JVM对象的内存分配,内存布局和访问定位
    记一次需求的表结构设计变更
    sql server中的系统数据库
    HttpWebRequest / HttpWebResponse 远程获取文件信息
    XML的操作
    对于XML无法传输转义字符的问题
    还原数备份文件 SQL语句
  • 原文地址:https://www.cnblogs.com/Westfalen/p/6130337.html
Copyright © 2020-2023  润新知