Paw*_*zur 6 android image bitmap android-6.0-marshmallow
初始代码是官方文档:Loading Large Bitmaps Efficiently。
我开始四处寻找,发现图像没有按照文档中的描述调整大小:
如果设置为 > 1 的值,则请求解码器对原始图像进行二次采样,返回较小的图像以节省内存。样本大小是对应于解码位图中单个像素的任一维度中的像素数。例如,inSampleSize == 4 返回一个图像,其宽度/高度是原始图像的 1/4,像素数是 1/16。任何 <= 1 的值都被视为与 1 相同。 注意:解码器使用基于 2 的幂的最终值,任何其他值将向下舍入到最接近的 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();
}
}
Run Code Online (Sandbox Code Playgroud)
运行代码的日志(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
Run Code Online (Sandbox Code Playgroud)
很好,现在有了真正的文件负载 ( 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
Run Code Online (Sandbox Code Playgroud)
我完全不解。如果您查看字节数,您可能会注意到,
我确信背后有愿景,为什么BitmapFactory
有decodeResource()
方法。我还没有完全弄清楚这个愿景是什么。
无论如何,使用decodeResource()
并不会消除您拥有的任何特定于密度的资源的密度转换。因此,如果您使用的是-hdpi
设备,并且您的可绘制对象的最佳匹配版本位于 中res/drawable-mdpi/
,inSampleSize
则密度转换都会发生。而且,坦率地说,我还没有花时间尝试弄清楚它们到底是如何结合起来的。
恕我直言,如果您要使用decodeResource()
,它应该用于与特定密度无关的东西:将其放入res/drawable-nodpi/
。但是,那只是我。
我应该从哪里去了解密度背后的魔力?
具体来说是关于BitmapFactory
?我不知道有任何关于此事的文章。
为什么它会根据 给出不同的尺寸
inJustDecodeBounds
?
没有任何线索,抱歉。
归档时间: |
|
查看次数: |
6609 次 |
最近记录: |