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 ...
随机推荐
- python 动态修改 类和实例 的方法
相信很多朋友在编程的时候都会想修改一下已经写好的程序行为代码,而最常见的方式就是通过子类来重写父类的一些不满足需求的方法.比如说下面这个例子. class Dog: def bark(self): p ...
- Python基础(二)数据类型
(一)数字 Python3中的数字类型分为3种,分别是整型,浮点型以及复数. Python2种的数字类型分为4种,分别是整型,长整型,浮点型以及复数. 其中长整型时Python2为应对位数较大的而设置 ...
- spring boot jpa 无法使用findOne
(findOne(id))说我无法转换成相应的类型,换一种即可,如下: user = userRepository.findOne(id);//spring 2.x用不了 @GetMapping(&q ...
- 莫烦大大keras的Mnist手写识别(5)----自编码
一.步骤: 导入包和读取数据 数据预处理 编码层和解码层的建立 + 构建模型 编译模型 训练模型 测试模型[只用编码层来画图] 二.代码: 1.导入包和读取数据 #导入相关的包 import nump ...
- HDU 4405 Aeroplane chess(概率dp,数学期望)
题目 http://kicd.blog.163.com/blog/static/126961911200910168335852/ 根据里面的例子,就可以很简单的写出来了,虽然我现在还是不是很理解为什 ...
- Linux思维导图之文件压缩
- 使用GitHub代码仓库Repositories上传自己的项目代码
1.下载客户端github(必须下载,需要该软件所提供的Git shell输入命令来上传项目)下载地址: https://github-windows.s3.amazonaws.com/GitHubS ...
- Django admin(四)一些有用定制
原文:https://www.cnblogs.com/linxiyue/p/4075048.html Model实例,myapp/models.py: 1 2 3 4 5 6 7 8 9 10 11 ...
- java的几种对象(PO,VO,DAO,BO,POJO)解释 (转)
java的几种对象(PO,VO,DAO,BO,POJO)解释 一.PO:persistant object 持久对象,可以看成是与数据库中的表相映射的java对象.最简单的PO就是对应数据库中某个表中 ...
- jQuery Webcam Plugin jscam.swf文件反编译工具使用说明
jQuery webcam plugin是一个在ie,firefox,chrome下都可以用的摄像头摄像及拍照用的插件. (http://www.xarg.org/project/jquery-web ...