android application级别的图片缓存
16lz
2022-04-19
缓存需要使用到SoftReference
step 1:定义ImageCahce
public class ImageCache{ private final HashMap> mSoftCache; public ImageCache(Context context) { mSoftCache = new HashMap>(); } public Bitmap get(String url) { final SoftReference ref = mSoftCache.get(url); if (ref == null) { return null; } Bitmap bitmap = ref.get(); if (bitmap == null) { mSoftCache.remove(url); } return bitmap; } public void put(String url, Bitmap bitmap) { mSoftCache.put(url, new SoftReference(bitmap)); } private void flush() { mSoftCache.clear(); } }
step 2:application 中定义ImageCache成员变量
private ImageCache mImageCache;public ImageCache getImageCache() { if (mImageCache == null) { mImageCache = new ImageCache(this); } return mImageCache; }
更多相关文章
- android截图代码
- 58. SaveVolley
- Android绘图API自定义View(三)
- Android(安卓)自定义Toast,不使用系统Toast
- 49 Android(安卓)fragment 自定义适配器adapter
- android 异步下载图片
- Android(安卓)自定义弹出框
- Android修改自定义Dialog的大小
- Android调用自定义Dialog中的控件