合理的应用图片缓存机制可以有效减少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)
站点信息
- 文章2312
- 用户1336
- 访客11623175
每日一句
Compliment yourself daily.
每天夸自己一句。
每天夸自己一句。
How to Ungroup Icons on Windows 11 Taskbar With a Registry Hack (and 2 More Ways)
反编译修改class文件变量
如何在大学成为一名优秀的程序员?
VMware NAT端口映射外网访问虚拟机linux
ubuntu下提取DSDT SSDT
使用HTML和CSS设计磨砂玻璃效果
解决android studio 4.4使用javah失败
vscode使用eslint自动代码格式化
c++浮点运算能力附安卓版
【源码】两种仪表盘
P2P中NAT之间的打洞可能性
jQuery的load方法Cannot read property 'indexOf' of undefined
【开源Roguelike游戏】素地牢源码
新会员