3、(imageFile,opts);设置inJustDecodeBounds为true后,decodeFile并不分配空间,但可计算出原始图片的长度和宽度,即opts.width和opts.height。有了这两个参数,再通过一定的算法,即可得到一个恰当的inSampleSize。查看Android源码,Android提供了一种动态计算的方法。?12345678910111213141516171819public static int computeSampleSize(BitmapFactory.Optionsoptions, int minSideLength,
4、int maxNumOfPixels){ int initialSize=computeInitialSampleSize(options,minSideLength, maxNumOfPixels); int roundedSize; if (initialSize<=8){ roundedSize=1; while (roundedSize
5、itialSize+7)/8 *8; } return roundedSize;} private static int computeInitialSampleSize(BitmapFactory.Optionsoptions,202122232425262728293031323334353637383940414243 int minSideLength,int maxNumOfPixels){ double w=options.outWidth; double h=options.outHeight; int lowerBo
6、und=(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
7、eisnooverlappingzone. return lowerBound; } if ((maxNumOfPixels==-1)&& (minSideLength==-1)){ return 1; }else if (minSideLength==-1){ return lowerBound; }else { return upperBound; }} 使用该算法