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;
    }
}

 

posted @ 2016-12-04 10:36  MarcoReus  阅读(1069)  评论(0编辑  收藏  举报