android避免decodeResource图片时占用太大的内存
2015-07-09 10:14
549 查看
增加largeHeap="true"属性。
android:largeHeap
Whether your application's processes should be created with a large Dalvik heap. This applies to all processes created for the application. It only applies to the first application loaded into a process; if you're using a shared user ID to allow multiple applications to use a process, they all must use this option consistently or they will have unpredictable results.
Most apps should not need this and should instead focus on reducing their overall memory usage for improved performance. Enabling this also does not guarantee a fixed increase in available memory, because some devices are constrained by their total available memory.
To query the available memory size at runtime, use the methods getMemoryClass() or getLargeMemoryClass().
我们知道调用BitmapFactory.decodeResource时,如果手机屏幕的密度很大时,如果只是在hdpi放了图片, decode出来的bitmap会自动的scale放大。 而且如果按照ARGB_8888模式decode的话一个像素需要4个字节,这样343*433分辨率的图片decode大概会占用2M多内存。 所以从2方面控制,一个是禁止scale, 一个是使用ALPHA_8模式decode。注意:这里需要看看效果是否ok, 因为使用低质量的模式decode图片可能会修饰一些图片的细节。
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201311/8f1f942f530a8ebeff8d8ca711309da2.png)
/**
* 因为目前我们只有一套资源文件,全都放在hdpi下面,这样如果是遇到高密度手机, 系统会按照
* scale = (float) targetDensity / density 把图片放到几倍,这样会使得在高密度手机上经常会发生OOM。
*
* 这个方法用来解决在如果密度大于hdpi(240)的手机上,decode资源文件被放大scale,内容浪费的问题。
* @param resources
* @param id
* @return
*/
public static Bitmap decodeResource(Resources resources, int id) {
int densityDpi = resources.getDisplayMetrics().densityDpi;
Bitmap bitmap;
TypedValue value = new TypedValue();
resources.openRawResource(id, value);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Bitmap.Config.ALPHA_8;
if (densityDpi > DisplayMetrics.DENSITY_HIGH) {
opts.inTargetDensity = value.density;
bitmap = BitmapFactory.decodeResource(resources, id, opts);
}else{
bitmap = BitmapFactory.decodeResource(resources, id);
}
return bitmap;
}
android:largeHeap
Whether your application's processes should be created with a large Dalvik heap. This applies to all processes created for the application. It only applies to the first application loaded into a process; if you're using a shared user ID to allow multiple applications to use a process, they all must use this option consistently or they will have unpredictable results.
Most apps should not need this and should instead focus on reducing their overall memory usage for improved performance. Enabling this also does not guarantee a fixed increase in available memory, because some devices are constrained by their total available memory.
To query the available memory size at runtime, use the methods getMemoryClass() or getLargeMemoryClass().
我们知道调用BitmapFactory.decodeResource时,如果手机屏幕的密度很大时,如果只是在hdpi放了图片, decode出来的bitmap会自动的scale放大。 而且如果按照ARGB_8888模式decode的话一个像素需要4个字节,这样343*433分辨率的图片decode大概会占用2M多内存。 所以从2方面控制,一个是禁止scale, 一个是使用ALPHA_8模式decode。注意:这里需要看看效果是否ok, 因为使用低质量的模式decode图片可能会修饰一些图片的细节。
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201311/8f1f942f530a8ebeff8d8ca711309da2.png)
/**
* 因为目前我们只有一套资源文件,全都放在hdpi下面,这样如果是遇到高密度手机, 系统会按照
* scale = (float) targetDensity / density 把图片放到几倍,这样会使得在高密度手机上经常会发生OOM。
*
* 这个方法用来解决在如果密度大于hdpi(240)的手机上,decode资源文件被放大scale,内容浪费的问题。
* @param resources
* @param id
* @return
*/
public static Bitmap decodeResource(Resources resources, int id) {
int densityDpi = resources.getDisplayMetrics().densityDpi;
Bitmap bitmap;
TypedValue value = new TypedValue();
resources.openRawResource(id, value);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Bitmap.Config.ALPHA_8;
if (densityDpi > DisplayMetrics.DENSITY_HIGH) {
opts.inTargetDensity = value.density;
bitmap = BitmapFactory.decodeResource(resources, id, opts);
}else{
bitmap = BitmapFactory.decodeResource(resources, id);
}
return bitmap;
}
相关文章推荐
- Android之ContentProvider总结
- Android基础类之BaseAdapter
- 制作Android手机开机动画的详细过程
- android 更新实现自己主动
- AndroidManifest.xml 详解
- Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface androi
- Android NDK(JNI)开发
- Android手势操作
- android菜鸟学习笔记18----Android数据存储(二)SharedPreferences
- android几种存储方式理解心得,
- Android入门(39)——第一章 使用Debug方式调试程序
- Android性能优化--开篇
- 第一个Android程序 -- HelloAndroid
- Android(java)学习笔记121:android.intent.action.MAIN 与 android.intent.category.LAUNCHER 理解
- ThinkAndroid框架——源码分享
- Android开发代码编写规范
- android adb通过wifi查看日志
- Android(java)学习笔记120:Android中的Application类用法
- Android的权限permission
- android启动的4种模式