合理的应用图片缓存机制可以有效减少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)
站点信息
- 文章2305
- 用户1336
- 访客11334404
每日一句
Life is more about giving than taking.
人生重在给予,而非索取。
人生重在给予,而非索取。
VirtualXposed,让你无需Root也能使用Xposed框架!
macOS系统盘爆满之罪魁祸首——向日葵远程
手机屏幕碎了怎么备份操作?
在Google Play商店中展示Android应用的八大技巧
利用Internet Download Manager下载Google云盘大文件
ndk神奇问题之non-numeric second argument to `wordlist' function: '8.7z'
font-awesome-to-png快速将Font-Awesome字体保存为PNG图片
Android Studio3.4.1更新及槽点
C++实现远程下载EXE并执行
Android上app_process启动java进程
C++ 11新语法获取系统盘符
软文实现月收入多几千的赚钱法
关于Android Studio不能查看源码
新会员