计算BitmapFactory.Options.inSampleSize的缩放比算法工具
2015-09-22 15:15
274 查看
来吧,直接上代码:
相关防止加载大图OOM的问题请移步:/article/1904195.html
—— lovey hy.
相关防止加载大图OOM的问题请移步:/article/1904195.html
package com.lzy.exploremessagedemo; import android.graphics.BitmapFactory; public class Util { /* * Compute the sample size as a function of minSideLength * and maxNumOfPixels. * minSideLength is used to specify that minimal width or height of a * bitmap. * maxNumOfPixels is used to specify the maximal size in pixels that is * tolerable in terms of memory usage. * * The function returns a sample size based on the constraints. * Both size and minSideLength can be passed in as IImage.UNCONSTRAINED, * which indicates no care of the corresponding constraint. * The functions prefers returning a sample size that * generates a smaller bitmap, unless minSideLength = IImage.UNCONSTRAINED. * * Also, the function rounds up the sample size to a power of 2 or multiple * of 8 because BitmapFactory only honors sample size this way. * For example, BitmapFactory downsamples an image by 2 even though the * request is 3. So we round up the sample size to avoid OOM. */ public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels); int roundedSize; if (initialSize <= 8) { roundedSize = 1; while (roundedSize < initialSize) { roundedSize <<= 1; } } else { roundedSize = (initialSize + 7) / 8 * 8; } return roundedSize; } private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { double w = options.outWidth; double h = options.outHeight; int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels)); int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength)); if (upperBound < lowerBound) { // return the larger one when there is no overlapping zone. return lowerBound; } if ((maxNumOfPixels == -1) && (minSideLength == -1)) { return 1; } else if (minSideLength == -1) { return lowerBound; } else { return upperBound; } } }
—— lovey hy.
相关文章推荐
- SpringMVC中Shiro报错org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible
- Linux系统下强制踢掉登录用户
- Linux shell判断文件和文件夹是否存在
- 文件编程之Linux下系统调用
- linux常用命令(48):scp命令
- Hadoop/Yarn/MapReduce内存分配(配置)方案
- Linux rpm 命令参数使用详解[介绍和应用]
- CentOS安装gdb 7.6.1教程
- Cooperate with Myself
- linux 下如何给一个普通用户有在指定目录下有创建文件的权限
- hdu 4349 Xiao Ming's Hope(C(n,m)的奇偶性)
- linux常用命令(47):rcp 命令
- 完全用GNU/Linux工作
- Linux常用(持续更新)
- apache中国镜像
- git bash 操作 从github上pull代码再从客户端push上去
- shell脚本编程-计算方式
- 利用facebook pop进行数字的动画显示。
- Linux中profile、bashrc、bash_profile之间的区别和联系
- 在OpenGL中进行多纹理贴图