Volley库的NetworkImageView怎么用

想要实现图片的双重缓存,

一、缓存在内存中

二、缓存在cache文件夹中

现在不知道NetworkImageView怎么用


现在 我贴个我自己用的代码:


第一步:创建一个ImageCache类

public class BFImageCache implements ImageLoader.ImageCache {
    /** 单例 */
    private static BFImageCache cache;
    /** 内存缓存 */
    private HashMap<String, Bitmap> memory;
    /** 缓存目录 */
    private String cacheDir;
    
    /** 获取单例 */
    public static BFImageCache getInstance() {
        if (null == cache) {
            cache = new BFImageCache();
        }
        return cache;
    }
    /** 初始化方法,Application的onCreate中调用 */
    public void initilize(Context context) {
        memory = new HashMap<String, Bitmap>();
        cacheDir = context.getCacheDir().toString()+File.separator;
    }
    
    @Override
    public Bitmap getBitmap(String url) {
        // 获取图片
        try {
            String key = MD5Util.toMd5HexString(url);
            if (memory.containsKey(key)) {
                return memory.get(key);
            } else {
                File file = new File(cacheDir + key);
                if (file.exists()) {
                    Bitmap bitmap = BitmapFactory.decodeFile(file.toString());
                    return bitmap;
                }
            }
        } catch (Exception e) {
            Log.d("halfman", e.getMessage());
        }
        return null;
    }
    @Override
    public void putBitmap(String url, Bitmap bitmap) {
        // 尺寸超过10时,清理缓存并放入内存
        if (memory.size() == 10) {
            Iterator<String> it = memory.keySet().iterator();
            while (it.hasNext()) {
                try {
                    String key = it.next();
                    Bitmap temp = memory.get(key);
                    File file = new File(cacheDir+key);
                    FileOutputStream os;
                    os = new FileOutputStream(file, false);
                    temp.compress(CompressFormat.JPEG, 100, os);
                    os.flush();
                    os.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            memory.clear();
        }
        // 放入图片到内存
        memory.put(MD5Util.toMd5HexString(url), bitmap);
    }
}

第二步:在Application中实例化一个RequestQueue

public static RequestQueue queue = Volley.newRequestQueue(context);

第三步:在Application的onCreate中初始化BFImageCache

BFImageCache.getInstance().initilize(this);

第四步:使用

imageLoader = new ImageLoader(RequestFactory.queue, BFImageCache.getInstance()); 


在要使用的Activity中初始化ImageLoader类:

imageLoader = new ImageLoader(RequestFactory.queue, BFImageCache.getInstance());

然后使用NetworkImageView发起网络图片请求

holder.image.setImageUrl("imageUrl", imageLoader);

http://bbs.ibeifeng.com/read-htm-tid-67421.html

推荐:android嵌入式开发:http://www.ibeifeng.com/goods-399.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值