android LruCache设置缓存的使用
16lz
2021-01-24
获取系统分配的内存大小
private int mTotalSize = (int) Runtime.getRuntime().totalMemory();
声明LruCache
private LruCache mLruCache ;
/*实例化LruCache*/ mLruCache = new LruCache(mTotalSize/5){ /*当缓存大于我们设定的最大值时,会调用这个方法,我们可以用来做内存释放操作*/ @Override protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) { super.entryRemoved(evicted, key, oldValue, newValue); if (evicted && oldValue != null){ oldValue.recycle(); } } /*创建 bitmap*/ @Override protected Bitmap create(String key) { final int resId = mViewPager.getResources().getIdentifier(key,"drawable", mViewPager.getContext().getPackageName()) ; return BitmapFactory.decodeResource(mViewPager.getResources(),resId) ; } /*获取每个 value 的大小*/ @Override protected int sizeOf(String key, Bitmap value) { return value.getByteCount(); } } ; }
获取bitmap
Bitmap bitmap = mLruCache.get(mData.get(position));
更多相关文章
- unity3d开发需要知道的一些常量
- picasso框架的使用
- android 缓存Bitmap - 开发文档翻译
- Android文件缓存的简单应用Acache
- 浅谈Android网络请求的前世今生-元基础HttpConnection
- Android使用post方式上传图片到服务器的方法
- Android常用工具整理
- android加载大量图片内存溢出的三种解决办法
- Android(安卓)-- ImageLoader简析