【问题标题】:Will Android automatically use inJustDecodeBounds when setting ImageView source in xml?Android在xml中设置ImageView源时会自动使用inJustDecodeBounds吗?
【发布时间】:2017-07-07 15:17:14
【问题描述】:

我知道,如果我正在加载图像并将其设置为 ImageView 中的源以编程方式,最好的办法是首先使用 inJustDecodeBounds 计算图像的大小,然后弄清楚如何我需要很多缩放,然后使用该缩放进行解码:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.my_drawable, options);

float srcHeight = options.outHeight;
float srcWidth = options.outWidth;

int inSampleSize = ...; // compute

options = new BitmapFactory.Options();
options.inSampleSize = inSampleSize;

Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_drawable, options);

myImageview.setImageBitmap(myBitmap);

现在,如果我想在 xml 中设置它并且我只在 /drawables 中提供一个基本可绘制对象/drawables-hdpi 中没有其他资产等)

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/my_drawable"/>

我知道 Android 会对生成的位图进行某种缩放以确保它适合,但它会像我自己解码位图一样有效地做到这一点吗?还是将整个位图加载到内存中并然后对其进行缩放?

换句话说,当 Android 留给自己的设备重新采样图像时,当我只提供一个基本的 /drawable 时,它这样做是否有效?

【问题讨论】:

    标签: android bitmap android-bitmap


    【解决方案1】:

    如果您确定所需的大小,自己缩放位图将节省内存,因为 android 不会这样做,至少不是您期望的方式(参见底部的编辑和 cmets)。

    我认为您还需要做一些其他的考虑。当您自己解码位图时,您的目标是一个大小,可能是ImageView 的大小(因此在计算出ImageView 的大小之后)。膨胀 xml 时,该大小尚不清楚。如果解码BitmapImageView 的大小发生变化,会发生什么情况?你会重新调整它吗?在这种情况下,解码更大的Bitmap 然后只缩放绘图不是更有效吗?

    找出android是否正在缩放Bitmap的一种方法非常简单,尝试在xml中设置一个可笑的大图像作为src,看看会发生什么(剧透警报,它会爆炸)。

    深入源码,看来ImageView的src的Bitmap是在Drawable类的这个方法中解码的,貌似没有考虑大小,只考虑屏幕密度

    public static Drawable createFromResourceStream(Resources res, TypedValue value,
            InputStream is, String srcName, BitmapFactory.Options opts) {
        if (is == null) {
            return null;
        }
    
        /*  ugh. The decodeStream contract is that we have already allocated
            the pad rect, but if the bitmap does not had a ninepatch chunk,
            then the pad will be ignored. If we could change this to lazily
            alloc/assign the rect, we could avoid the GC churn of making new
            Rects only to drop them on the floor.
        */
        Rect pad = new Rect();
    
        // Special stuff for compatibility mode: if the target density is not
        // the same as the display density, but the resource -is- the same as
        // the display density, then don't scale it down to the target density.
        // This allows us to load the system's density-correct resources into
        // an application in compatibility mode, without scaling those down
        // to the compatibility density only to have them scaled back up when
        // drawn to the screen.
        if (opts == null) opts = new BitmapFactory.Options();
        opts.inScreenDensity = Drawable.resolveDensity(res, 0);
        Bitmap  bm = BitmapFactory.decodeResourceStream(res, value, is, pad, opts);
        if (bm != null) {
            byte[] np = bm.getNinePatchChunk();
            if (np == null || !NinePatch.isNinePatchChunk(np)) {
                np = null;
                pad = null;
            }
    
            final Rect opticalInsets = new Rect();
            bm.getOpticalInsets(opticalInsets);
            return drawableFromBitmap(res, bm, np, pad, opticalInsets, srcName);
        }
        return null;
    }
    

    另外,在BitmapDrawabledraw(Canvas canvas) 方法中,绘图是用这条线完成的:

    canvas.drawBitmap(bitmap, null, mDstRect, paint);
    

    看起来Bitmap 按原样使用,它是目标Rect 进行缩放

    编辑

    今天我也无意中发现了另一个有趣的事情,我在drawable文件夹中放了一张图片(未指定密度),并检索到一个带有BitmapFactory.decodeResourceStream(res, value, is, pad, opts)的Bitmap,和上面的一样。我得到的位图比可绘制文件夹中的图像大得多,我认为如果未指定密度,它将假定为 mdpi,甚至会对位图进行上采样以获得更高的目标密度。

    【讨论】:

    • 使用巨大的 png 进行测试的好主意。
    • 谢谢,准备看它爆炸 :) 我今天也发现了一些新东西,我把它放在了答案中
    • 是的,尝试了一个巨大的图像和:boom:。此外,您所说的未指定密度的图像大小在这个类似的问题中有所暗示:stackoverflow.com/a/13120950/1650674。如果你输入 drawable-nodpi 会发生什么?
    • 嗯,好问题,根据this one,这就是你告诉android不要缩放图像的方式!我想我不会再使用 assets 文件夹了:P
    猜你喜欢
    • 2012-11-11
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多