Bitmap_图片内存缓存(2)
来源:互联网 发布:inpho软件 编辑:程序博客网 时间:2024/05/21 08:01
昨天写了关于BitMap关于图片压缩的博客, 今天写一下关于图片的内存缓存 。这些文章网上很多,与其每次找别人的博客,还不如自己写一篇 。加深记忆
都是一些比较老的技术了,没什么好解释的 ,看看就懂了 。
还是来一张妹子镇楼
//=======================================================================
图片缓存类
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); }}
阅读全文
0 0
- Bitmap_图片内存缓存(2)
- Bitmap_图片硬盘缓存(3)
- Bitmap_多级缓存实现图片加载
- Bitmap_关于图片压缩(1)
- Android 图片内存缓存
- 图片内存缓存
- Android图片内存缓存
- Android内存图片缓存
- Android图片之三级缓存(着重讲内存缓存)
- ImageView(图片缓存内存中+图片二次采样)
- 图片的内存缓存控制
- 内存缓存图片及原理
- ListView 图片加载+内存缓存+图片压缩
- ImageCache——图片三级缓存(内存、文件、网络)
- Android-图片三级缓存(网络-本地-内存)
- Android中图片的三级缓存(内存、文件、网络)
- android自定义图片三级缓存(内存、SD卡、网络)
- Android--图片加载处理(内存溢出和三级缓存)
- Android中AIDL的使用(二)
- QDialog 可移动
- C++ 一些小的知识点
- Android Studio LibraryModule中引用aar
- 前端之jquery常见方法
- Bitmap_图片内存缓存(2)
- java设计模式之二:策略模式
- faster-rcnn demo.py详解
- 关闭TransactionSynchronizationManager下日志打印
- 金蝶K3 Wise版本BOM批量多级展开代码
- 操作系统引论
- java for/foreach/while 3种循环性能比较
- iOS调试——解决playground无法执行的问题
- 二叉树的最大节点