缓存图片并显示在adapter

利用imageloader去处理图片

public class MusicAdapter extends BaseAdapter {
    private List<Music> musics;
    private Context context;
    private LayoutInflater inflater;
    private ImageLoader imageLoader;
    private RequestQueue queue;

    public MusicAdapter(List<Music> musics, Context context) {
        super();
        this.musics = musics;
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        queue = Volley.newRequestQueue(context);
        imageLoader = new ImageLoader(queue, new BitmapCache());
    }

加载图片,设置缓存,适合加载多个图片       

holder = (ViewHolder) convertView.getTag();

        Music music = getItem(position);
        holder.tvName.setText(music.getTitle());
        // 使用volley加载图片
        ImageListener listener = ImageLoader.getImageListener(holder.ivPic, R.drawable.ic_launcher,
                R.drawable.ic_launcher);
        imageLoader.get(music.getPic_small(), listener);
        return convertView;


设置缓存:把图片先加载在缓存区

public class BitmapCache implements ImageCache {


        private LruCache<String, Bitmap> mCache;

        public BitmapCache() {
            int maxSize = 10 * 1024 * 1024;
            mCache = new LruCache<String, Bitmap>(maxSize) {
                @Override
                protected int sizeOf(String key, Bitmap bitmap) {
                    return bitmap.getRowBytes() * bitmap.getHeight();
                }
            };
        }

        @Override
        public Bitmap getBitmap(String url) {
            return mCache.get(url);
        }

        @Override
        public void putBitmap(String url, Bitmap bitmap) {
            mCache.put(url, bitmap);
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值