合理的应用图片缓存机制可以有效减少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
- 访客11478256
每日一句
Success is built on failed years.
成功皆筑于经年败绩。
成功皆筑于经年败绩。
Autonomous NAT Traversal
解决SSH连接问题packet too long 1349676920
Android Studio开发之app:transformJackWithJackForDebug错误
MPAndroidChart标记控件MarkerView的使用方法
叶子F墙正式上线
SpringBoot打Jar包did not assign a file to the build artifact
谷歌Pixel正在开始起飞?
不要错过的9.0高分催泪国漫《白蛇:缘起》
Android Studio3.0引入第三方库文件的总结
Android工程集成华为扫码SDK
关于iOS最常见的15个问题
VC实现的QQ自动停靠隐藏功能
imencode和imdecode使用
新会员