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从TXT创建PDF文件——reportlab
使用reportlab创建PDF文件电子书一般都是txt格式的,某些电子阅读器不能读取txt的文档,如DPT-RP1.因此本文从使用python实现txt到pdf的转换,并且支持生成目录,目录能够生成 ...
- eoLinker上线两周年+ AMS V4.0 发布:全新UI界面,带来领先的API开发管理解决方案!
2018年7月,eoLinker 发布了<eoLinker AMS 2018年年中用户调研问卷>,前后经历一周的时间,共收集到超过1000份有效调查问卷.超过300个有效改进意见. eoL ...
- codeforces 427D Match & Catch(后缀数组,字符串)
题目 参考:http://blog.csdn.net/xiefubao/article/details/24934617 题意:给两个字符串,求一个最短的子串.使得这个子串在两个字符串中出现的次数都等 ...
- C#那20道题
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 使用GitHub代码仓库Repositories上传自己的项目代码
1.下载客户端github(必须下载,需要该软件所提供的Git shell输入命令来上传项目)下载地址: https://github-windows.s3.amazonaws.com/GitHubS ...
- Problem 20
Problem 20 问题网址:https://projecteuler.net/problem=20 n! means n × (n − 1) × ... × 3 × 2 × 1阶乘For exam ...
- 通用 mapper
一.为什么需要通用 mapper 插件 通用 mapper 插件可以自动的生成 sql 语句. 虽然 mybatis 有逆向工程,可以直接生成 XxxMapper.xml 文件,但是这种生成的方式存在 ...
- 基于Mysql-Proxy 实现MariaDB 读写分离
一.Mysql-Proxy 简单介绍 MySQL-Proxy是一个处于你的client端和MySQL server端之间的简单程序,它可以监测.分析或改变它们的通信.它使用灵活,没有限制,常见的用途包 ...
- poj 3041 最小点覆盖=最大匹配
#include<stdio.h> #include<string.h> #define N 510 int map[N][N],n,mark[N],link[N]; in ...
- Spring MVC-静态页面示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_static_pages.htm 说明:示例基于Spring MVC 4.1.6. ...