通过BitmapFactorydecode方法设置特定的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);
}
}

最新文章

  1. 使用rowid抽取数据方法以及大数据量游标卡住的应对
  2. MVC中使用EF:排序,过滤,分页
  3. 关于s:iterator 和s:if 的结合使用
  4. freebsd|odoo - 为odoo报表 安装文泉译中文字体
  5. Objective-C Polymorphism
  6. nyist 676 小明的求助
  7. 如何将两个列表变成一个python字典
  8. [生成树][Uva1395][Slim Span]
  9. 上海赛区-org.apache.ibatis.type.TypeException: JDBC requires that the JdbcType must be specified for all nullable parameters.
  10. crypto加密
  11. [js高手之路]封装运动框架实战左右与上下滑动的焦点轮播图
  12. java萌新尝试搭建WordPress记录
  13. java.lang.SecurityException: Prohibited package name:
  14. Nacos环境搭建
  15. springboot情操陶冶-jmx解析
  16. RPA与AI_新技术能给企业业务流程带来怎样的价值?
  17. java 中 byte[]、File、InputStream 互相转换
  18. linux C遍历目录下文件
  19. tar 的–exclude参数,实现不包括某些文件(转)
  20. 第9月第26天 pairs和ipairs cocos2dx 动画

热门文章

  1. appium的等待
  2. 15.3 Task Task.Yield和Task.Delay说明
  3. 一次由于 MTU 设置不当导致的网络访问超时
  4. CentOS7.2安装nginx失败
  5. python-windows环境安装
  6. noip模板复习
  7. 1.sts的下载安装
  8. Automatic Tuning of Memory Management
  9. find结合rm删除大量文件
  10. [Codeup 25482]选美