昨天写了关于BitMap关于图片压缩的博客, 今天写一下关于图片的内存缓存 。这些文章网上很多,与其每次找别人的博客,还不如自己写一篇 。加深记忆

都是一些比较老的技术了,没什么好解释的 ,看看就懂了 。

还是来一张妹子镇楼

Bitmap_图片内存缓存(2)




//=======================================================================

   图片缓存类

public class LruCacheUtil {

    Context context;
    LruCache<String, Bitmap> mMemoryCache;
    public static final String TAG = "LruCacheUtil";

    public LruCacheUtil(Context context) {
        this.context = context.getApplicationContext();
        int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);  //获取手机的最大内存
        int cacheSize = maxMemory / 8;                                    //使用内存的1/8用来做图片缓存
        mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
            }
        };
    }

    /***
     * 清理缓存
     */
    public void clearCache() {
        if (mMemoryCache != null) {
            if (mMemoryCache.size() > 0) {
                Log.d(TAG, "mMemoryCache.size() " + mMemoryCache.size());
                mMemoryCache.evictAll();
                Log.d(TAG, "mMemoryCache.size()" + mMemoryCache.size());
            }
            mMemoryCache = null;
        }
    }

    /***
     * 添加图片到缓存
     * @param key
     * @param bitmap
     */
    public synchronized void addBitmapToMemoryCache(String key, Bitmap bitmap) {
        if (mMemoryCache.get(key) == null) {
            if (key != null && bitmap != null)
                mMemoryCache.put(key, bitmap);
            Log.w(TAG, "添加图片到缓存");
        } else
            Log.w(TAG, "图片缓存已经存在");
    }

    /***
     * 从缓存中去拿图片
     * @param key
     * @return
     */
    public synchronized Bitmap getBitmapFromMemCache(String key) {
        try {
            Bitmap bm = mMemoryCache.get(key);
            if (key != null) {
                return bm;
            }
        } catch (Exception e) {
            return null;
        }
        return null;
    }

    /**
     * 移除缓存
     *
     * @param key
     */
    public synchronized void removeImageCache(String key) {
        if (key != null) {
            if (mMemoryCache != null) {
                Bitmap bm = mMemoryCache.remove(key);
                if (bm != null)
                    bm.recycle();
            }
        }
    }
}

//==============================================

图片压缩类,上一章节也讲过

public class ImageResizer {

    public ImageResizer() {
    }

    /***
     * 从资源文件中读取图片资源
     * @param resources
     * @param resId
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public Bitmap decodeSampleBitmapFactoryFromResource(Resources resources, int resId, int reqWidth, int reqHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;   //去加载图片
        BitmapFactory.decodeResource(resources, resId, options);
        options.inSampleSize = calculateImSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(resources, resId, options);
    }

    public Bitmap decodeBitmapFromFileDescriptor(FileDescriptor fd, int reqWidth, int reqheight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFileDescriptor(fd, null, options);
        options.inSampleSize = calculateImSampleSize(options, reqWidth, reqheight);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFileDescriptor(fd, null, options);
    }

    /***
     * 计算图片的压缩比例
     * @param options
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    private int calculateImSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        if (reqHeight == 0 || reqWidth == 0) {
            return 1;
        }
        int height = options.outHeight;
        int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || reqWidth > reqWidth) {
            int halfWidth = width / 2;
            int halfHeight = height / 2;
            while ((halfHeight / inSampleSize) >= reqHeight || (halfWidth / inSampleSize) >= reqWidth) {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }

======================================================

主界面的调用类 ,用了清理缓存 ,延迟两秒,大家有兴趣可以自己看下效果,

public class MainActivity extends AppCompatActivity {

    private ImageView iv_image;
    LruCacheUtil lruCacheUtil;
    private Handler handler = new Handler();
    ImageResizer mImageResizer;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intView();
    }

    private void intView() {
        mImageResizer = new ImageResizer();
        lruCacheUtil = new LruCacheUtil(MainActivity.this);
        Bitmap map = mImageResizer.decodeSampleBitmapFactoryFromResource(getResources(), R.mipmap.aa, 500, 500);
        lruCacheUtil.addBitmapToMemoryCache("123456", map);
        iv_image = (ImageView) findViewById(R.id.iv_image);

        Bitmap mapShow = lruCacheUtil.getBitmapFromMemCache("123456");
        iv_image.setImageBitmap(mapShow);

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                lruCacheUtil.clearCache();
                Bitmap mapShow = lruCacheUtil.getBitmapFromMemCache("123456");
                iv_image.setImageBitmap(mapShow);
            }
        }, 2000);
    }
}





相关文章:

  • 2021-10-22
  • 2022-03-09
  • 2021-07-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-24
  • 2021-07-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
  • 2021-09-08
相关资源
相似解决方案