合理的应用图片缓存机制可以有效减少oom异常。
Bitmap进行缩放的工具类
使用lrucache对bitmap进行内存缓存的类
Bitmap进行缩放的工具类
import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; /** * 压缩图片 */ public class BitmapUtils { /** * 根据资源id获取到图片,并进行压缩 * @param res * @param resId * @param reqWidth * @param reqHeight * @return */ public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, opts); int inSampleSize = cacluateInSampleSize(opts, reqWidth, reqHeight); opts.inSampleSize = inSampleSize; opts.inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory.decodeResource(res, resId, opts); return bitmap; } /** * 从byte数组中获取图片并压缩 * @param data * @param reqWidth * @param reqHeight * @return */ public static Bitmap decodeSampledBitmapFromByteArray(byte[] data, int reqWidth, int reqHeight) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(data, 0, data.length, opts); int inSampleSize = cacluateInSampleSize(opts, reqWidth, reqHeight); opts.inJustDecodeBounds = false; opts.inSampleSize = inSampleSize; Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts); return bitmap; } private static int cacluateInSampleSize(BitmapFactory.Options opts, int reqWidth, int reqHeight) { if (opts == null) return 1; int inSampleSize = 1; int realWidth = opts.outWidth; int realHeight = opts.outHeight; if (realHeight > reqHeight || realWidth > reqWidth) { int heightRatio = realHeight / reqHeight; int widthRatio = realWidth / reqWidth; inSampleSize = (heightRatio > widthRatio) ? widthRatio : heightRatio; } return inSampleSize; } }
使用lrucache对bitmap进行内存缓存的类
import android.graphics.Bitmap; import android.support.v4.util.LruCache; import android.util.Log; /** * *使用lrucache缓存图片到内存,做成了单例模式 */ public class BitmapLruCacheHelper { private static final String TAG = null; private static BitmapLruCacheHelper instance = new BitmapLruCacheHelper(); private LruCache < string, bitmap > cache = null; private BitmapLruCacheHelper() { int maxSize = (int)(Runtime.getRuntime().maxMemory() / 8); cache = new LruCache < string, bitmap = "" > (maxSize) { @Override protected int sizeOf(String key, Bitmap value) { return value.getRowBytes() * value.getHeight(); } }; } /** *加入缓存 * @param key * @param value */ public void addBitmapToMemCache(String key, Bitmap value) { if (key == null || value == null) { return; } if (cache != null && getBitmapFromMemCache(key) == null) { cache.put(key, value); Log.i(TAG, "put to lrucache success"); } } /** * 从缓存中获取图片 * @param key * @return */ public Bitmap getBitmapFromMemCache(String key) { if (key == null) { return null; } Bitmap bitmap = cache.get(key); Log.i(TAG, "from lrucache,bitmap=" + bitmap); return bitmap; } /** * 获取实例 * @return */ public static BitmapLruCacheHelper getInstance() { return instance; } }
收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2313
- 用户1336
- 访客11736654
每日一句
Frost opens the curtain on winter.
霜降拉开冬天的序幕
霜降拉开冬天的序幕
8位数QQ官方注册过滤法
3D游戏调节游戏帧率
已经存在的Android Studio工程添加NDK支持
Android8.0系统中通知栏的适配
ndk神奇问题之non-numeric second argument to `wordlist' function: '8.7z'
解决SSH连接问题packet too long 1349676920
Swift标准类库,相当于一些C ++里的STL类库
程序员应该使用Linux的7个理由
【教程】手把手教你开通淘小铺赚佣金
i7 8700K+GTX1070安装黑苹果10.13.6
关于iOS最常见的15个问题
Android利用canvas画各种图形
网络爬虫Scrapy入门
新会员