以下是来自Google的this link提供的方法。
constructor (props){
super(props);
this.handleClick = this.handleClick.bind(this);
}
如果有500 * 500的图片需要调整为100 * 100,则此代码段的结果为4,因为它们使用public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
和halfWidth
。但如果我理解正确,结果应为8.我认为代码应该修改为:
halfHeight
任何人都能解释一下吗?我发现他们已经多次修改过这段代码了,但似乎仍然错了?
答案 0 :(得分:1)
这个想法是图像按两个步骤按比例缩小:首先是2的最大功率小于或等于所需的下采样因子,然后是小于2的数量,以最终请求结束图像大小。
如果需要将500 * 500图像调整为100 * 100,则此片段的结果为4,因为它们使用halfWidth和halfHeight。但如果我理解正确,结果应为8.
如果比例因子为8,那么500x500像素图像将缩小到62x62像素,然后需要按比例放大到所需的100x100像素大小。
正如代码评论所说:
计算最大的inSampleSize值,该值为2的幂,并保持高度和宽度都大于请求的高度和宽度。
这将是4,因为那时你将得到一个125x125像素的图像,这个图像大于请求的高度和宽度100x100像素。我们的想法是,你想在最后一步缩小比例,而不是缩小太远,然后不得不缩小比例并获得模糊的图像。
所需的下采样因子为5,小于或等于2的最高功率为4.