博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图片压缩及缓存解决OOM
阅读量:3763 次
发布时间:2019-05-22

本文共 2952 字,大约阅读时间需要 9 分钟。

1.使用BitmapOptions 压缩图片
  1. //根据图片质量确定每个像素点所占字节数  
  2. public static int getBytesPerPixel(Bitmap.Config config) {    
  3.     if (config == Bitmap.Config.ARGB_8888) {    
  4.         return 4;    
  5.     } else if (config == Bitmap.Config.RGB_565) {    
  6.         return 2;    
  7.     } else if (config == Bitmap.Config.ARGB_4444) {    
  8.         return 2;    
  9.     } else if (config == Bitmap.Config.ALPHA_8) {    
  10.         return 1;    
  11.     }   
  12.     return 1;    
  13. }  
  14. public static long getBitmapSizeInMemory(int imageW, int imageH) {    
  15.  return imageH * imageW * getBytesPerPixel(Bitmap.Config.ARGB_8888);    
  16. }  
  17. //获取的inSampleSize必须是2的倍数 ps:本函数(getScaleInSampleSize)只是一种参考,具体实现还需要根据实际情况有所动 
  18. public static int getScaleInSampleSize(int resW, int resH, int desW, int desH) {       
  19. int scaleW = resW / desW;       
  20. int scaleH = resH / desH;        
  21. int largeScale = scaleH > scaleW ? scaleH : scaleW;       
  22. int sampleSize = 1;       
  23. while (sampleSize < largeScale) {            
  24. sampleSize *= 2;        
  25. }      
  26. return sampleSize;    
  27. }  
  28. //获取压缩图片  
  29. public static Bitmap decodeBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) 
  30. {
      
  31. // 第一次解析将inJustDecodeBounds设置为true,来获取图片信息   
  32. final BitmapFactory.Options options = new BitmapFactory.Options();        
  33. options.inJustDecodeBounds = true;      
  34. BitmapFactory.decodeResource(res, resId, options);        
  35. int height = options.outHeight;        
  36. int width = options.outWidth;        
  37. String mimeType = options.outMimeType;       
  38. // 调用上面定义的方法计算inSampleSize值 
  39.        options.inSampleSize = getScaleInSampleSize(width, height, reqWidth, reqHeight); 
  40. //options.inPreferredConfig= Bitmap.Config.RGB_565; //如有必要,还可以修改图片质量,进一步减小图片大小      
  41. // 使用获取到的inSampleSize值再次解析图片        
  42. options.inJustDecodeBounds = false;        
  43. return BitmapFactory.decodeResource(res, resId, options);    
  44. }  
2.使用LruCache:
  1. private LruCache<String, Bitmap> mMemoryCache;  
  2. @Override  
  3. protected void onCreate(Bundle savedInstanceState) {  
  4.     // 获取到可用内存的最大值,使用内存超出这个值会引起OutOfMemory异常。  
  5.     // LruCache通过构造函数传入缓存值,以KB为单位。  
  6.     int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);  
  7.     // 使用最大可用内存值的1/6作为缓存的大小。  
  8.     int cacheSize = maxMemory / 6;  
  9.     mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {  
  10.         @Override  
  11.         protected int sizeOf(String key, Bitmap bitmap) {  
  12.             // 重写此方法来衡量每张图片的大小,默认返回图片数量。  
  13.             return bitmap.getByteCount() / 1024;  
  14.         }  
  15.     };  
  16. }  
  17. public void addBitmapToMemoryCache(String key, Bitmap bitmap) {  
  18.     if (getBitmapFromMemCache(key) == null) {  
  19.         mMemoryCache.put(key, bitmap);  
  20.     }  
  21. }  
  22. public Bitmap getBitmapFromMemCache(String key) {  
  23.     return mMemoryCache.get(key);  
  24. }  
  25. public void loadBitmap(int resId, ImageView imageView) {  
  26.     final String imageKey = String.valueOf(resId);  
  27.     final Bitmap bitmap = getBitmapFromMemCache(imageKey);  
  28.     if (bitmap != null) {  
  29.         imageView.setImageBitmap(bitmap);  
  30.     } else {  
  31.         //如果缓存里面没有就使用默认的图片  
  32.         imageView.setImageResource(R.drawable.image_default);  
  33.         LoadWorkerTask task = new LoadWorkerTask();  
  34.         task.execute(resId);  
  35.     }  
  36. }  
  37. class LoadWorkerTask extends AsyncTask<Integer, Void, Bitmap> {  
  38.     // 在后台加载图片。  
  39.     @Override  
  40.     protected Bitmap doInBackground(Integer... params) {  
  41.         final Bitmap bitmap = decodeBitmapFromResource(  
  42.                 getResources(), params[0], 100100);  
  43.         addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);  
  44.         return bitmap;  
  45.     }  
  46. }  

转载地址:http://qbfpn.baihongyu.com/

你可能感兴趣的文章
Linux系统 实操题 添加磁盘、格式化磁盘、挂载、目录、权限等操作
查看>>
python 模块的用法 用户登录界面 加 盐值混淆
查看>>
python 深浅拷贝和赋值
查看>>
C进制转换
查看>>
JDBC java连接数据库 初体验
查看>>
c语言—实现扫雷游戏
查看>>
java 函数
查看>>
BUUCTF——刷新过的图片
查看>>
$_SERVER[‘QUERY_STRING‘] 学习记录
查看>>
Python练习——回文数
查看>>
Python练习——Turtle库学习记录
查看>>
前端面试题2021-css
查看>>
冒泡排序
查看>>
ES6中类的定义
查看>>
vue3.0的router页面跳转
查看>>
C/C++内存区域划分详解
查看>>
mysql数据库安装资源、步骤及基本操作指令详解
查看>>
解决Powershell前面没有base,无法激活虚拟环境问题
查看>>
出现“qt.qpa.plugin: Could not load the Qt platform plugin “xcb“ in ‘ ’even though it was found
查看>>
windows系统,安装cuda11.0+cudnn8.0记录
查看>>