为什么inSampleSize中的BitmapFactory.Options有时不向下舍入到最接近2的幂?

时间:2016-06-02 16:06:38

标签: android android-bitmap

正如官方文件所说the decoder uses a final value based on powers of 2, any other value will be rounded down to the nearest power of 2.",但今天我做了一个测试,发现有不同的东西,这是我的测试代码:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.image, options);
int w = options.outWidth;
int h = options.outHeight;
Log.i("Log", "originW: " + w + "  originH: " + h);
for (int i = 0; i < 16; i++) {
    BitmapFactory.Options scaleOption = new BitmapFactory.Options();
    scaleOption.inSampleSize = i;
    scaleOption.inScaled=false;
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image, scaleOption);
    double rw = (w * 1.0 / bitmap.getWidth());
    double rh = (h * 1.0 / bitmap.getHeight());
    DecimalFormat format = new DecimalFormat("0.00");
    Log.i("Log", "width: \t" + bitmap.getWidth() +'\t'+ "  height: \t" + bitmap.getHeight() +'\t'+
            "  originW/scaleH: \t" + format.format(rw) +'\t'+ " originH/scaleH: \t" + format.format(rh) +'\t'+ "   inSampleSize: \t" + i);
}

我发现结果与我传入的图像不同:
1. this image not work normal, you can download and have a try

,输出为:

I/Log: width: 2247 height:  1264  originW/scaleH: 1.00  originH/scaleH: 1.00    inSampleSize: 0
I/Log: width: 2247 height:  1264  originW/scaleH: 1.00  originH/scaleH: 1.00    inSampleSize: 1
I/Log: width: 1123 height:  632   originW/scaleH: 2.00  originH/scaleH: 2.00    inSampleSize: 2
I/Log: width: 749  height:  421   originW/scaleH: 3.00  originH/scaleH: 3.00    inSampleSize: 3
I/Log: width: 561  height:  316   originW/scaleH: 4.01  originH/scaleH: 4.00    inSampleSize: 4
I/Log: width: 449  height:  252   originW/scaleH: 5.00  originH/scaleH: 5.02    inSampleSize: 5
I/Log: width: 374  height:  210   originW/scaleH: 6.01  originH/scaleH: 6.02    inSampleSize: 6
I/Log: width: 321  height:  180   originW/scaleH: 7.00  originH/scaleH: 7.02    inSampleSize: 7
I/Log: width: 280  height:  158   originW/scaleH: 8.02  originH/scaleH: 8.00    inSampleSize: 8
I/Log: width: 249  height:  140   originW/scaleH: 9.02  originH/scaleH: 9.03    inSampleSize: 9
I/Log: width: 224  height:  126   originW/scaleH: 10.03 originH/scaleH: 10.03   inSampleSize: 10
I/Log: width: 204  height:  114   originW/scaleH: 11.01 originH/scaleH: 11.09   inSampleSize: 11
I/Log: width: 187  height:  105   originW/scaleH: 12.02 originH/scaleH: 12.04   inSampleSize: 12
I/Log: width: 172  height:  97    originW/scaleH: 13.06 originH/scaleH: 13.03   inSampleSize: 13
I/Log: width: 160  height:  90    originW/scaleH: 14.04 originH/scaleH: 14.04   inSampleSize: 14
I/Log: width: 149  height:  84    originW/scaleH: 15.08 originH/scaleH: 15.05   inSampleSize: 15

所以任何人都知道问题在哪里?

0 个答案:

没有答案