全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Android 优化Bitmap避免OutOfMemoryError使用android提供的BitmapFactory解码图片时,往往会因为图片过大而遇到OutOfMemoryError的异常。要想正常使用,一种简便的方式是分配更少的内存空间来存储,即在载入图片的时候以牺牲图片质量为代价,将图片进行放缩,这是一种避免OOM所采用的解决方法。但是,这种方法是得不偿失的,牺牲了图片质量。在BitmapFactory中有一个内部类BitmapFactory.Options,其中值得我们注意的是inSampleSize和inJustDecodeBounds两个属性:inSampleSize是以2的指数的倒数被进行放缩If set to a value 1, requests the decoder to subsample the original image, returning a smaller image to save memory. (1 - decodes full size; 2 - decodes 1/4th size; 4 - decode 1/16th size). Because you rarely need to show and have full size bitmap images on your phone. For manipulations smaller sizes are usually enough.inJustDecodeBounds为Boolean型设置inJustDecodeBounds为true后,decodeFile并不分配空间,但可计算出原始图片的长度和宽度,即options.outWidth和options.outHeight。要对图片进行缩放,最大的问题就是怎么在运行时动态的改变inSampleSize的值,通过上面的inJustDecodeBounds可以知道图片原始的大小,那么这样以来就可以通过算法来得到一个恰当的inSampleSize值。其动态算法可参考下面的,网上也很多,大体都一样:/* compute Sample Size* param options* param minSideLength* param maxNumOfPixels* return*/ public static int computeSampleSize(BitmapFactory.Options options,int minSideLength,int maxNumOfPixels) int initialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels);int roundedSize;if (initialSize =8) roundedSize = 1;while (roundedSize initialSize) roundedSize = 1; else roundedSize = (initialSize + 7) /8 *8;return roundedSize;/* compute Initial Sample Size* param options* param minSideLength* param maxNumOfPixels* return*/ private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength,int maxNumOfPixels) double w = options.outWidth;double h = options.outHeight;/ 上下限范围 int lowerBound = (maxNumOfPixels = -1) ?1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels);int upperBound = (minSideLength = -1) ?128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength);if (upperBound lowerBound) / return the larger one when there is no overlapping zone. return lowerBound;if (maxNumOfPixels = -1) & (minSideLength = -1) return 1; else if (minSideLength = -1) return lowerBound; else return upperBound;有了上面的算法,我们就可以轻易的get到Bitmap了:/* get Bitmap* param imgFile* param minSideLength* param maxNumOfPixels* return*/ public static Bitmap tryGetBitmap(String imgFile,int minSideLength,int maxNumOfPixels) if (imgFile =null | imgFile.length() =0)return null;try FileDescriptor fd = new FileInputStream(imgFile).getFD();BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;/ BitmapFactory.decodeFile(imgFile, options); BitmapFactory.decodeFileDescriptor(fd, null, options);options.inSampleSize = computeSampleSize(options, minSideLength,maxNumOfPixels);try / 这里一定要将其设置回false,因为之前我们将其设置成了true / 设置inJustDecodeBounds为true后,decodeFile并不分配空间,即,BitmapFactory解码出来的Bitmap为Null,但可计算出原始图片的长度和宽度 options.inJustDecodeBounds = false;Bitmap bmp =
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 初中生演讲稿励志篇
- 初中生物活动方案
- 成都市 2024-2025 学年小学五年级语文期中专项训练模拟卷及答案
- 2025年食欲不振试题及答案
- 2025年湖南省公务员面试模拟训练真题
- 2025年预防医学招聘试题及答案
- 2025年花卉栽培培训试题及答案
- 2025年高中二年级化学上学期有机化学试卷
- 2025年江西省公务员考试申论真题试卷
- 2025钢筋供应商采购合同模板
- 人教版小学1-6年级诗词全集
- 青少年网络安全法制课
- 精神专科医院建筑设计规范
- 《石墨烯新材料》课件
- 阶段性测试卷(范围:第一、二、三章)(基础篇)(秋季讲义)(人教A版2019必修第一册)
- 新一代信息技术产业发展新趋势与关键路径研究
- 数学期中模拟测试卷八年级上册(考试范围:第12-14章)(原卷版)
- 坐井观天二年级上册教学设计方案
- 风电场与养殖场生态互补关系研究
- 初中道德与法治教师教学能力水平考核测试试题(含答案)
- 2024年共青团入团积极分子团校结业考试试题库及答案
评论
0/150
提交评论