0

请看以下代码:

public Pixmap newPixmap(String fileName, PixmapFormat format) {
        Config config = null;
        if (format == PixmapFormat.RGB565)
            config = Config.RGB_565;
        else if (format == PixmapFormat.ARGB4444)
            config = Config.ARGB_4444;
        else
            config = Config.ARGB_8888;

        Options options = new Options();
        options.inPreferredConfig = config;

        InputStream in = null;
        Bitmap bitmap = null;
        try {
            in = assets.open(fileName);
            bitmap = BitmapFactory.decodeStream(in);
            if (bitmap == null)
                throw new RuntimeException("Couldn't load bitmap from asset '"
                        + fileName + "'");
        } catch (IOException e) {
            throw new RuntimeException("Couldn't load bitmap from asset '"
                    + fileName + "'");
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }

        if (bitmap.getConfig() == Config.RGB_565)
            format = PixmapFormat.RGB565;
        else if (bitmap.getConfig() == Config.ARGB_4444)
            format = PixmapFormat.ARGB4444;
        else
            format = PixmapFormat.ARGB8888;

        return new AndroidPixmap(bitmap, format);
    }

我不明白这部分:

Options options = new Options();
options.inPreferredConfig = config;

看起来,程序员试图配置加载位图的格式。我知道 Options-Class 是 BitmapFactory 的嵌套类。

但是在代码中的任何地方都使用了对象选项。为什么?

当我在加载位图之前使用选项对象配置格式时,为什么会有一个获取格式的 if 请求?

我很困惑。谢谢您的帮助。

4

2 回答 2

0

我不知道您的代码来自哪里,但如果您想要(我怀疑您确实)想要使用该Options对象,那么您应该像这样更改您bitmap = BitmapFactory.decodeStream(in);的使用它:bitmap = BitmapFactory.decodeStream(in, null, options);

于 2013-10-16T16:24:54.313 回答
0

The decoder will try to decode into this internal configuration for this: options.inPreferredConfig = config; Basically config is: A bitmap configuration describes how pixels are stored. This affects the quality (color depth) as well as the ability to display transparent/translucent colors. Read this: http://developer.android.com/reference/android/graphics/Bitmap.Config.html

于 2013-10-16T16:26:21.360 回答