Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用BitmapFactory.options?_Java_Android_Bitmap_Options_Bitmapfactory - Fatal编程技术网

Java 如何使用BitmapFactory.options?

Java 如何使用BitmapFactory.options?,java,android,bitmap,options,bitmapfactory,Java,Android,Bitmap,Options,Bitmapfactory,请看以下代码: public Pixmap newPixmap(String fileName, PixmapFormat format) { Config config = null; if (format == PixmapFormat.RGB565) config = Config.RGB_565; else if (format == PixmapFormat.ARGB4444) config

请看以下代码:

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类是BitmapFactory的嵌套类

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

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


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

我不知道您的代码来自何处,但如果您想(我怀疑您确实想)使用
选项
对象,那么您应该更改
位图=BitmapFactory.decodeStream(in)
这样使用它:
位图=BitmapFactory.decodeStream(in,null,options)

解码器将尝试解码为此内部配置: options.inPreferredConfig=config; 基本上,配置是: 位图配置描述如何存储像素。这会影响质量(颜色深度)以及显示透明/半透明颜色的能力。 请阅读以下内容:

此代码取自一本书,因此我对这些不必要的行感到困惑。我问自己,为什么作者没有使用位图decodeFile方法(字符串路径名,选项选项)?如果他使用这种方法,代码会短20行,不是吗?