Android Bitmap那些事之如何优化内存


BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds =true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;


public static int calculateInSampleSize(
BitmapFactory.Options options,int reqWidth,int reqHeight){
// Raw height and width of image
finalint height = options.outHeight;
finalint width = options.outWidth;
int inSampleSize =1; if(height > reqHeight || width > reqWidth){ finalint halfHeight = height /2;
finalint halfWidth = width /2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while((halfHeight / inSampleSize)> reqHeight
&&(halfWidth / inSampleSize)> reqWidth){
inSampleSize *=2;
}
} return inSampleSize;
}

在decode的时候先设置options.inJustDecodeBounds =true,获取到图片参数后再设置为false,这就是decode时的技巧,下面就把完整代码贴出来,可以作为工具方法来使用:

public static Bitmap decodeSampledBitmapFromResource(Resources res,int resId,
int reqWidth,int reqHeight){ // First decode with inJustDecodeBounds=true to check dimensions
finalBitmapFactory.Options options =newBitmapFactory.Options();
options.inJustDecodeBounds =true;
BitmapFactory.decodeResource(res, resId, options); // Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set
options.inJustDecodeBounds =false;
returnBitmapFactory.decodeResource(res, resId, options);
}

上面的方法来自于google官网,没必要进行修改,这就是程序员的拿来主义吧,关键在于要知道为什么这么写。下面是我自己写的一个方法可以直接拿来当工具用。

/**
* 对图片进行压缩,主要是为了解决控件显示过大图片占用内存造成OOM问题,一般压缩后的图片大小应该和用来展示它的控件大小相近.
*
* @param context 上下文
* @param resId 图片资源Id
* @param reqWidth 期望压缩的宽度
* @param reqHeight 期望压缩的高度
* @return 压缩后的图片
*/
public static Bitmap compressBitmapFromResourse(Context context, int resId, int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
/*
* 第一次解析时,inJustDecodeBounds设置为true,
* 禁止为bitmap分配内存,虽然bitmap返回值为空,但可以获取图片大小
*/
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(context.getResources(), resId, options); final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
options.inSampleSize = inSampleSize;
// 使用计算得到的inSampleSize值再次解析图片
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(context.getResources(), resId, options);
}

Android Bitmap那些事之如何优化内存的更多相关文章
- Android性能优化之巧用软引用与弱引用优化内存使用
前言: 从事Android开发的同学都知道移动设备的内存使用是非常敏感的话题,今天我们来看下如何使用软引用与弱引用来优化内存使用.下面来理解几个概念. 1.StrongReference(强引用) 强 ...
- Bitmap那些事之内存占用计算和加载注意事项
前言:本来我是做电视应用的,但是因为公司要出手机,人员紧张,所以就抽调我去支援一下,谁叫俺是雷锋呢!我做的一个功能就是处理手机中的应用ICON,处理无非就是美化一下,重新与底板进行合成和裁剪,用到了很 ...
- Android性能优化-内存优化
原文链接 Manage Your App’s Memory 前言 在任何软件开发环境中,RAM都是比较珍贵的资源.在移动操作系统上更是这样,因为它们的物理内存通常受限.尽管在ART和Dalvik虚拟机 ...
- Android 性能优化 ---- 内存优化
1.Android内存管理机制 1.1 Java内存分配模型 先上一张JVM将内存划分区域的图 程序计数器:存储当前线程执行目标方法执行到第几行. 栈内存:Java栈中存放的是一个个栈帧,每个栈帧对应 ...
- 谷歌发布 Android 8.1 首个开发者预览版,优化内存效率
今晨,谷歌推出了 Android 8.1 首个开发者预览版,此次升级涵盖了针对多个功能的提升优化,其中包含对 Android Go (设备运行内存小于等于 1 GB)和加速设备上对机器学习的全新神经网 ...
- 【腾讯bugly干货分享】Android自绘动画实现与优化实战——以Tencent OS录音机波形动
前言 本文为腾讯bugly的原创内容,非经过本文作者同意禁止转载,原文地址为:http://bugly.qq.com/bbs/forum.php?mod=viewthread&tid=1180 ...
- Android——BitMap(位图)相关知识总结贴
Android中文API(136) —— Bitmap http://www.apkbus.com/android-54644-1-1.html Android 4.0 r1 API—Bitmap(S ...
- Android开发笔记——常见BUG类型之内存泄露与线程安全
本文内容来源于最近一次内部分享的总结,没来得及详细整理,见谅. 本次分享主要对内存泄露和线程安全这两个问题进行一些说明,内部代码扫描发现的BUG大致分为四类:1)空指针:2)除0:3)内存.资源泄露: ...
- (转)Android开发:性能最佳实践-管理应用内存
翻自:http://developer.android.com/training/articles/memory.html 在任何软件开发环境中,RAM都是宝贵的资源,但在移动操作系统中更加珍贵.尽管 ...
随机推荐
- Microsoft SQL Server 2008 基本安装说明
Microsoft SQL Server 2008 基本安装说明 安装SQL2008的过程与SQL2005的程序基本一样,只不过在安装的过程中部分选项有所改变,当然如果只熟悉SQL2000安装的同志来 ...
- 【转】浅析linux内存模型
转自:http://pengpeng.iteye.com/blog/875521 0. 内存基本知识 我们通常称 linux的内存子系统为:虚拟内存子系统(virtual memory system) ...
- Java学习笔记之深入理解引用
引言:Java中数据传递的方式,除了基本数据类型是按照值传递,其它类型全部是按照引用传递,这和C++有很大区别,但是很多网上文章都解释的不清楚,甚至是错误的,在查阅资料之后,下面整理出一个比较容易理解 ...
- Swift学习笔记十四
Deinitialization 当类的实例对象即将要被释放时,会立即调用deinitializer,通过deinit关键字来定义deinitializer,和initializer一样,它也只存在于 ...
- codeforces Gym 100187A A. Potion of Immortality
A. Potion of Immortality Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1001 ...
- Codeforces Round #312 (Div. 2) A. Lala Land and Apple Trees 暴力
A. Lala Land and Apple Trees Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/cont ...
- P、NP、NP-Complete、NP-hard问题
Table of Contents 1 遇到难题怎么办? 2 什么是P.NP.NP-Complete和NP-hard 3 P = NP ???? 4 参考 1 遇到难题怎么办? 遇到一个问题,通常我们 ...
- jquery ajax请求了两次问题
页面有一个请求form: <form method="post" id="signin"> <label for="orderid& ...
- java 访问修饰符
此题考察修饰符,函数定义等,故从网上搜罗了下相关资料,总结如下: 类.方法.成员变量和局部变量的可用修饰符 修饰符 类 成员访求 构造方法 成员变量 局部变量 abstract(抽象的) √ √ - ...
- WPF 之 后台设置Image的Souce
后台动态设置Image的Souce. 方法一: BitmapImage imgSource = new BitmapImage(new Uri("location",UriKind ...