android bitmap内存处理
2014-09-19 09:40
309 查看
程序中一次性加载多张图片,而且还要适应屏幕,这就造成内存紧张,进程会出现OOM,特别是现在的手机分辨率高达1920*1024,一张图片占用内存高达8M,首先要获取原图片的高宽
//减少内存
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inPurgeable = true; //设置图片可以被回收
//options.inTempStorage=new byte[12 * 1024];//听说是开启临时存储
options.inInputShareable = true;
Bitmap backgroudBitMap = BitmapFactory.decodeStream(new FileInputStream(file),null,options);
然后可获取高宽
float imgWidth = backgroudBitMap.getWidth();
float imgHeight = backgroudBitMap.getHeight();
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(backgroudBitMap, 0, 0,(int) imgWidth, (int) imgHeight, matrix, true);
在然后可以根据新的图片获取大小并且用Canvas画图了
现在原图可以回收了backgroudBitMap.recycle();
backgroudBitMap = null;
因为新的图片要设置成背景图,不能现在回收,图片要是缩小还好,要是放大则占用很大的内存,如果是1-3张还好,多了之后就会内存溢出了,大家有什么好的方法代替createBitmap进行缩放,或者有什么好的方法管理内存,大家共同学习进步。
//减少内存
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inPurgeable = true; //设置图片可以被回收
//options.inTempStorage=new byte[12 * 1024];//听说是开启临时存储
options.inInputShareable = true;
Bitmap backgroudBitMap = BitmapFactory.decodeStream(new FileInputStream(file),null,options);
然后可获取高宽
float imgWidth = backgroudBitMap.getWidth();
float imgHeight = backgroudBitMap.getHeight();
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(backgroudBitMap, 0, 0,(int) imgWidth, (int) imgHeight, matrix, true);
在然后可以根据新的图片获取大小并且用Canvas画图了
现在原图可以回收了backgroudBitMap.recycle();
backgroudBitMap = null;
因为新的图片要设置成背景图,不能现在回收,图片要是缩小还好,要是放大则占用很大的内存,如果是1-3张还好,多了之后就会内存溢出了,大家有什么好的方法代替createBitmap进行缩放,或者有什么好的方法管理内存,大家共同学习进步。
相关文章推荐
- android Bitmap处理以 节省内存的方法
- Android Bitmap图片处理,防止内存溢出
- android 内存溢出处理办法(出现java.lang.OutOfMemoryError: bitmap size exceeds VM budget 异常)
- Android有效的处理Bitmap,减少内存
- Android Bitmap操作内存问题总结(图片处理、截屏等)
- (android图像处理)android之bitmap各种常用函数
- Bitmap占用内存过多的处理
- Android 避免bitmap内存限制
- Android Bitmap内存限制问题
- Android 避免bitmap内存限制
- 解决Android BitmapOutOfMemory 内存泄露
- Android Bitmap内存限制
- Android-避免出现bitmap内存限制OUT OF MEMORY的一种方法
- ANDROID BITMAP内存限制OOM,OUT OF MEMORY
- Android Bitmap内存限制OOM Out Of Memory
- Android-避免出现bitmap内存限制OUT OF MEMORY的一种方法
- android 使用Dalvik的setTargetHeapUtilization 增强程序堆内存的处理效率
- Android-避免出现bitmap内存限制OUT OF MEMORY的一种方法
- Android Bitmap 图片处理细节
- java弱引用(WeakReference)和SoftReference的区别以及在android内存处理的作用