通过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高效加载大图的更多相关文章

  1. Android高效加载大图、多图解决方案,有效避免程序内存溢出现象

    好久没有写博客了,今天就先写一个小的关于在Android中加载大图如何避免内存溢出的问题. 后面会写如何使用缓存技术的核心类,android.support.v4.util.LruCache来加载图片 ...

  2. Android高效加载大图、多图解决方案,有效避免程序OOM

    高效加载大图片 我们在编写Android程序的时候经常要用到许多图片,不同图片总是会有不同的形状.不同的大小,但在大多数情况下,这些图片都会大于我们程序所需要的大小.比如说系统图片库里展示的图片大都是 ...

  3. Android高效加载大图、多图解决方案,有效避免程序OOM(转)

    本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工,英文好的朋友也可以直接去读原文. http://developer.android.com/training/displaying ...

  4. Android高效加载大图,多图解决方案,有效避免程序OOM异常

    收藏自:http://blog.csdn.net/guolin_blog/article/details/9316683 谷歌官方文档:http://developer.android.com/tra ...

  5. android 高效加载大图

    在写代码的时候就已经解释: /** * 计算samplesize的大小 * @param options 传一个BitmapFactory.Options 进去获取图片的大小和类型 * @param ...

  6. Android图片加载框架最全解析(三),深入探究Glide的缓存机制

    在本系列的上一篇文章中,我带着大家一起阅读了一遍Glide的源码,初步了解了这个强大的图片加载框架的基本执行流程. 不过,上一篇文章只能说是比较粗略地阅读了Glide整个执行流程方面的源码,搞明白了G ...

  7. Android图片加载框架最全解析(一),Glide的基本用法

    现在Android上的图片加载框架非常成熟,从最早的老牌图片加载框架UniversalImageLoader,到后来Google推出的Volley,再到后来的新兴军Glide和Picasso,当然还有 ...

  8. Android中高效的显示图片之一 ——加载大图

    在网上看了不少文章,发现还是官方文档介绍最详细,把重要的东西简单摘要出来.详细可看官方文档地址 ( http://www.bangchui.org/read.php?tid=9 ) . 在应用中显示图 ...

  9. Android开发之高效加载Bitmap

    一.概述 在Android开发中,我们经常与Bitmap打交道,而对Bitmap的不恰当的操作经常会导致OOM(Out of Memory).这篇文章我们会介绍如何高效地在Android开发中使用Bi ...

随机推荐

  1. 【转载】解决方案:git@github.com出现Permission denied (publickey)

    遇到的问题 今天心血来潮,想将intellij上的项目代码放到GitHub上管理. 在进行添加远程库的时候,出现了:git@github.com出现Permission denied (publick ...

  2. Centos7从3.10升级内核到4.9后无法启动解决办法:mpt[23]sas驱动问题

    Centos7升级内核后无法启动解决办法:mpt[23]sas驱动问题 前言 这个问题存在有一段时间了,之前做的centos7的ISO,在进行内核的升级以后就存在这个问题: 系统盘在板载sata口上是 ...

  3. 为 Elasticsearch 添加中文分词,对比分词器效果

    转自:http://keenwon.com/1404.html 为 Elasticsearch 添加中文分词,对比分词器效果 Posted in 后端 By KeenWon On 2014年12月12 ...

  4. SDOI2017数字表格

    求$\prod_{i=1}^n\prod_{j=1}^n\text{Fib}[\gcd(i,j)]\;\text{mod}\;10^9+7$的值 令$n\leq m$,则有: \begin{align ...

  5. [Ynoi2011]D1T1

    题目大意: 给定一个序列$a_1,a_2,\dots,a_n$,进行$m$次操作,每次操作如下: 1. 给定$x,y,z$,对所有下标为$y,y+x,y+2x,\dots$的元素加上$z$(保证$y\ ...

  6. python项目开发:用RabbitMQ实现异步RPC

    程序要求: 1. 用Rabbit MQ实现RPC 1. 可以异步地执行多条命令 2. 可以对一次性对多个机器执行命令 程序效果: --->run dir host1 host2 .... --- ...

  7. laravel5 文件上传

    <!DOCTYPE html><html lang="zh-CN"><head> {{--设置字符编码--}} <meta charset ...

  8. django orm多条件查询及except处理不存在记录的样码

    日常所用的功能, 记录下来加深记忆. try: sv_set = Sv.objects.get(project=site_name, version=deploy_version) print sv_ ...

  9. mysql绑定多个ip地址

    http://jpuyy.com/2013/07/mysql-bind-multi-address.html mysql绑定多个ip地址 发表于2013 年 7 月 1 日 my.cnf中有选项bin ...

  10. unity游戏开发之NGUI的UISprite染色

    游戏的UI开发中常常会遇到染色问题.比如button失效变灰的效果,同一个道具通过策划表配的颜色值染上红绿蓝紫等颜色,效果例如以下 最笨最挫的方法当然是让美术多出几个资源图.这种一个缺点是浪费资源,在 ...