• 如何使用软引用


    1. 什么是软引用:http://en.wikipedia.org/wiki/Soft_reference
    1. 使用软引用:

    我构造了简单的HashMap来存储SoftReference的对象。

          private HashMap<Integer, SoftReference<Bitmap>> mThumbnailCache = null;//新建软引用缓存。

     

    //创建一个软应用

          private SoftReference<Bitmap> createSoftReference(int albumId) {

               if (null == mCacheManager)

                     return null;

               Bitmap bmpTmp = mCacheManager.getThumbnail(getAlbumPos(albumId));

               if (null == bmpTmp)

                     return null;

                SoftReference<Bitmap> sr = new SoftReference<Bitmap>(bmpTmp);

               bmpTmp = null;//需要置空

               return sr;

          }

     

    //获得软引用缓存中的Bitmap

          public Bitmap getThumbnail(int albumId) {

               if (null == mCacheManager || null == mThumbnailCache)

                     return null;

               if (mThumbnailCache.containsKey(albumId)) {

                     SoftReference<Bitmap> bmpSr = mThumbnailCache.get(albumId);

                     if (null != bmpSr && null != bmpSr.get())

                          return bmpSr.get();

                     mThumbnailCache.remove(albumId);

               }

    //如果GC释放了一个SoftReference,则需要重新创建SoftReference,并且将该SoftReference存到HashMap里。

               SoftReference<Bitmap> sr = createSoftReference(albumId);

               if (null == sr)

                     return null;

               mThumbnailCache.put(albumId, sr);

               return sr.get();

          }

  • 相关阅读:
    学习了一下调色理论
    几个同步软件
    慢性咽炎
    flash行情
    C#创建Windows服务
    .net下 foreach 与 for 的效率比较测试
    HTML服务器控件与Web服务器控件的区别
    c#遍历HashTable
    ASP.NET中Server与Request对象的方法
    .net内存回收与Dispose﹐Close﹐Finalize方法
  • 原文地址:https://www.cnblogs.com/androidwsjisji/p/2231349.html
Copyright © 2020-2023  润新知