gpt4 book ai didi

android - Bitmap.Options.inSampleSize 应该如何工作?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:22:02 24 4
gpt4 key购买 nike

初始代码是官方文档:Loading Large Bitmaps Efficiently .

我开始四处寻找,发现图像不会按照 documentation 中的描述调整大小。 :

If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1. Note: 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(res, resId, options);
Log.e(LOG_TAG, "orig img size " + options.outWidth + "x" +
options.outHeight);

// Calculate inSampleSize
// options.inJustDecodeBounds = false; // version 2
for (int i = 2; i<20; i*=2) {
options.inSampleSize = i;
Log.d(LOG_TAG, "inSampleSize: " + options.inSampleSize);
Bitmap b = BitmapFactory.decodeResource(res, resId, options);
Log.e(LOG_TAG, "img size "+options.outWidth+"x"+options.outHeight);
if (b != null) {
Log.e(LOG_TAG, "real img size " + b.getWidth() + "x" +
b.getHeight() + " byte count " + b.getByteCount());
b.recycle();
}
}

运行代码的日志(Nexus 5、Android 6.0):

E/t: view size 1080x1776
E/t: orig img size 2448x3264
D/t: inSampleSize: 2
E/t: img size 1224x1632
D/t: inSampleSize: 4
E/t: img size 612x816
D/t: inSampleSize: 8
E/t: img size 306x408
D/t: inSampleSize: 16
E/t: img size 153x204
D/t: inSampleSize: 32
E/t: img size 228x306
E/t: real img size 228x306 byte count 279072

很好,现在有了真正的文件加载(inJustDecodeBounds=false):

E/t: view size 1080x1776
E/t: orig img size 2448x3264
D/t: inSampleSize: 2
W/art: Throwing OutOfMemoryError "Failed to allocate a 71912460 byte allocation with 1048576 free bytes and 62MB until OOM"
D/t: inSampleSize: 4
E/t: img size 1836x2448
E/t: real img size 1836x2448 byte count 17978112
D/t: inSampleSize: 8
E/t: img size 918x1224
E/t: real img size 918x1224 byte count 4494528
D/t: inSampleSize: 16
E/t: img size 459x612
E/t: real img size 459x612 byte count 1123632
D/t: inSampleSize: 32
E/t: img size 228x306
E/t: real img size 228x306 byte count 279072

我完全不解。如果您查看字节数,您可能会注意到,

最佳答案

我确信 BitmapFactory 有一个 decodeResource() 方法背后有一个愿景。我还没有完全弄清楚那个愿景是什么。

无论如何,使用 decodeResource() 不会消除您拥有的任何密度特定资源的密度转换。因此,如果您使用的是 -hdpi 设备,并且您的 drawable 的最佳匹配版本在 res/drawable-mdpi/ 中,inSampleSize 和密度转换都将发生。而且,坦率地说,我还没有花时间去弄清楚它们是如何结合在一起的。

恕我直言,如果你打算使用 decodeResource(),它应该用于与特定密度无关的东西:把它放在 res/drawable-nodpi/。但是,那只是我。

Where should I look to learn about the magic behind the density?

一般情况下?有 the documentationthe other documentation .

特别是关于BitmapFactory?我不知道有任何关于此的文章。

Why does it give different sizes depending on inJustDecodeBounds?

没有线索,抱歉。

关于android - Bitmap.Options.inSampleSize 应该如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33714992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com