ITPub博客

首页 > 应用开发 > IT综合 > Volley库的NetworkImageView怎么用

Volley库的NetworkImageView怎么用

原创 IT综合 作者:itxiyuan 时间:2014-06-27 17:30:16 0 删除 编辑
想要实现图片的双重缓存,

一、缓存在内存中

二、缓存在cache文件夹中

现在不知道NetworkImageView怎么用?


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


第一步:创建一个ImageCache类

点击(此处)折叠或打开

  1. public class BFImageCache implements ImageLoader.ImageCache {
  2.     /** 单例 */
  3.     private static BFImageCache cache;
  4.     /** 内存缓存 */
  5.     private HashMap<String, Bitmap> memory;
  6.     /** 缓存目录 */
  7.     private String cacheDir;
  8.     
  9.     /** 获取单例 */
  10.     public static BFImageCache getInstance() {
  11.         if (null == cache) {
  12.             cache = new BFImageCache();
  13.         }
  14.         return cache;
  15.     }
  16.     /** 初始化方法,Application的onCreate中调用 */
  17.     public void initilize(Context context) {
  18.         memory = new HashMap<String, Bitmap>();
  19.         cacheDir = context.getCacheDir().toString()+File.separator;
  20.     }
  21.     
  22.     @Override
  23.     public Bitmap getBitmap(String url) {
  24.         // 获取图片
  25.         try {
  26.             String key = MD5Util.toMd5HexString(url);
  27.             if (memory.containsKey(key)) {
  28.                 return memory.get(key);
  29.             } else {
  30.                 File file = new File(cacheDir + key);
  31.                 if (file.exists()) {
  32.                     Bitmap bitmap = BitmapFactory.decodeFile(file.toString());
  33.                     return bitmap;
  34.                 }
  35.             }
  36.         } catch (Exception e) {
  37.             Log.d(\"halfman\", e.getMessage());
  38.         }
  39.         return null;
  40.     }
  41.     @Override
  42.     public void putBitmap(String url, Bitmap bitmap) {
  43.         // 尺寸超过10时,清理缓存并放入内存
  44.         if (memory.size() == 10) {
  45.             Iterator<String> it = memory.keySet().iterator();
  46.             while (it.hasNext()) {
  47.                 try {
  48.                     String key = it.next();
  49.                     Bitmap temp = memory.get(key);
  50.                     File file = new File(cacheDir+key);
  51.                     FileOutputStream os;
  52.                     os = new FileOutputStream(file, false);
  53.                     temp.compress(CompressFormat.JPEG, 100, os);
  54.                     os.flush();
  55.                     os.close();
  56.                 } catch (Exception e) {
  57.                     e.printStackTrace();
  58.                 }
  59.             }
  60.             memory.clear();
  61.         }
  62.         // 放入图片到内存
  63.         memory.put(MD5Util.toMd5HexString(url), bitmap);
  64.     }
  65. }
第二步:在Application中实例化一个RequestQueue

点击(此处)折叠或打开

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

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

点击(此处)折叠或打开

  1. BFImageCache.getInstance().initilize(this)

第四步:使用

点击(此处)折叠或打开

  1. 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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29694782/viewspace-1196556/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2014-06-06

  • 博文量
    2
  • 访问量
    25924