Android高效加载大图
通过
BitmapFactory的decode方法设置特定的options缩小图片到指定尺寸
- 1:通过加载设置了只编码图片边界options的图片,获取原图的尺寸和类型
- 2:计算图片需要缩小的倍数
- 3:设置options的inSimpleSize属性为缩小的倍数
- 4:获取缩小之后的Bitmap
options.inJustDecodeBounds = true;
设置会在decode时,不分配内存,但是可以获取图片的尺寸和类型
options.inSampleSize属性设置图片的缩小倍数
If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory.The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1. Note: the decoder uses a final value based on powers of 2, any other value will be rounded down to the nearest power of 2.
如果设置的值大于1,解码器就会从原始图片中抽取返回一个缩小的图片储存在内存中。这个SimpleSize值是每个维度中的像素数,对应于被解码的图片的单个像素。例如,SimpleSize==4,就会返回一个宽或高是原图1/4的图片,像素则是原图的1/16。小于1的值相当于1。解码器使用基于2的幂的数值,对于任何SimpleSize不是2的幂的值都会被和谐到最接近2的值。
public class BitmapTool {
//读取图片尺寸和类型
private static BitmapFactory.Options getBitmapOptionsOnlyWidthAndHeight(String imagePath) {
BitmapFactory.Options options = new BitmapFactory.Options();
//设置为true之后,decode**方法将会不分配内存,但是可以获取图片的宽高,类型
options.inJustDecodeBounds = true; //just decode bounds意味这只加载边界
BitmapFactory.decodeFile(imagePath, options);
return options;
}
//获取图片应该被缩小的倍数
private static int getScaleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int imageWidth = options.outWidth;
final int imageHeight = options.outHeight;
int scaleSize = 1;
//如果图片的实际尺寸大于了实际需要的,就计算缩放倍数
if (imageWidth > reqWidth || imageHeight > reqHeight) {
final int halfWidth = imageWidth / 2;
final int halfHeight = imageHeight / 2;
while ((halfWidth / scaleSize) > reqWidth && (halfHeight / scaleSize) > reqHeight) {
scaleSize *= 2;
}
}
return scaleSize;
}
/**
* 从资源中加载图片
*
* @param reqWidth 目标宽
* @param reqHeight 目标高
* @return 缩小之后的图片
*/
public static Bitmap getBitmapFromResource(Context context, int id, int reqWidth, int reqHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
//设置为true之后,decode**方法将会不分配内存,但是可以获取图片的宽高,类型
options.inJustDecodeBounds = true; //just decode bounds意味这只加载边界
BitmapFactory.decodeResource(context.getResources(), id, options);
options.inSampleSize = getScaleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(context.getResources(), id, options);
}
}
Android高效加载大图的更多相关文章
- Android高效加载大图、多图解决方案,有效避免程序内存溢出现象
好久没有写博客了,今天就先写一个小的关于在Android中加载大图如何避免内存溢出的问题. 后面会写如何使用缓存技术的核心类,android.support.v4.util.LruCache来加载图片 ...
- Android高效加载大图、多图解决方案,有效避免程序OOM
高效加载大图片 我们在编写Android程序的时候经常要用到许多图片,不同图片总是会有不同的形状.不同的大小,但在大多数情况下,这些图片都会大于我们程序所需要的大小.比如说系统图片库里展示的图片大都是 ...
- Android高效加载大图、多图解决方案,有效避免程序OOM(转)
本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工,英文好的朋友也可以直接去读原文. http://developer.android.com/training/displaying ...
- Android高效加载大图,多图解决方案,有效避免程序OOM异常
收藏自:http://blog.csdn.net/guolin_blog/article/details/9316683 谷歌官方文档:http://developer.android.com/tra ...
- android 高效加载大图
在写代码的时候就已经解释: /** * 计算samplesize的大小 * @param options 传一个BitmapFactory.Options 进去获取图片的大小和类型 * @param ...
- Android图片加载框架最全解析(三),深入探究Glide的缓存机制
在本系列的上一篇文章中,我带着大家一起阅读了一遍Glide的源码,初步了解了这个强大的图片加载框架的基本执行流程. 不过,上一篇文章只能说是比较粗略地阅读了Glide整个执行流程方面的源码,搞明白了G ...
- Android图片加载框架最全解析(一),Glide的基本用法
现在Android上的图片加载框架非常成熟,从最早的老牌图片加载框架UniversalImageLoader,到后来Google推出的Volley,再到后来的新兴军Glide和Picasso,当然还有 ...
- Android中高效的显示图片之一 ——加载大图
在网上看了不少文章,发现还是官方文档介绍最详细,把重要的东西简单摘要出来.详细可看官方文档地址 ( http://www.bangchui.org/read.php?tid=9 ) . 在应用中显示图 ...
- Android开发之高效加载Bitmap
一.概述 在Android开发中,我们经常与Bitmap打交道,而对Bitmap的不恰当的操作经常会导致OOM(Out of Memory).这篇文章我们会介绍如何高效地在Android开发中使用Bi ...
随机推荐
- PAT_A1142#Maximal Clique
Source: PAT A1142 Maximal Clique (25 分) Description: A clique is a subset of vertices of an undirect ...
- PAT_A1123#Is It a Complete AVL Tree
Source: PAT A1123 Is It a Complete AVL Tree (30 分) Description: An AVL tree is a self-balancing bina ...
- Socket编程(day14)
一.基于TCP传输层的编程模型 TCP是面向连接的,安全可靠的. 三次握手 服务器端编程模型 .创建一个用于网络通讯的设备 通讯端点 socket() #include <sys/types.h ...
- JavaEE el表达式中三目运算符的使用
也可以通过在bean对象中写getter方法通过对象.属性进行调用
- [bzoj1860 ZJOI2006] 超级麻将 (线性dp)
传送门 Description Input 第一行一个整数N(N<=100),表示玩了N次超级麻将. 接下来N行,每行100个数a1..a100,描述每次玩牌手中各种牌的数量.ai表示数字为i的 ...
- 2013 - lost connection to mysql server at 'reading initial communication packet' 连接mysql报错
早上刚到公司,启动项目发现连接池初始化报错,于是我打开本地mysql管理工具,测试是否可以连接.报错2013代码: 现已解决. 重启服务器mysql服务就好. 因为我连接的是本地windows系统,所 ...
- [bzoj3062][Usaco13Feb]Taxi_贪心
Taxi bzoj-3062 Usaco13Feb 题目大意:有n个奶牛想坐出租车.第i头奶牛在起点a[i]等候,想坐出租车到b[i].Bessie从0出车,车上只能坐一头奶牛.她必须完成所有奶牛的要 ...
- [bzoj2049][Sdoi2008]Cave 洞穴勘测_LCT
Cave 洞穴勘测 bzoj-2049 Sdoi-2008 题目大意:维护一个数据结构,支持森林中加边,删边,求两点连通性.n个点,m个操作. 注释:$1\le n\le 10^4$,$1\le m\ ...
- 洛谷 P2896 [USACO08FEB]一起吃饭Eating Together
P2896 [USACO08FEB]一起吃饭Eating Together 题目描述 The cows are so very silly about their dinner partners. T ...
- POJ 3225
基本参考http://blog.csdn.net/metalseed/article/details/8039326 总的来说,敲完一遍理解会更加好一点,标记下传法. U:把区间[l,r]覆盖成1I: ...