以下是由google提供的this link的方法。InSampleSize在Android中計算錯誤嗎?
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;
}
如果有其需要被調整大小爲100 * 100 500 * 500圖像,此代碼段的結果是4,因爲它們使用和halfWidth
halfHeight
。但是,如果我理解正確的話,結果應該是8.我覺得代碼應該修改爲:
while ((halfHeight/inSampleSize) > reqHeight
&& (halfWidth/inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
inSampleSize *= 2;
任何人都可以解釋一下嗎?我發現他們已經多次修改了這段代碼,但似乎仍然是錯誤的?
謝謝,解釋是正確的。但我覺得所要求的規模應該是你無法跨越的底線。對於700 * 700的圖像,重新採樣的尺寸爲175 * 175,似乎太大。這是模糊和記憶之間的折衷。 –