2013-10-16 67 views
0

請看看下面的代碼:如何使用BitmapFactory.options?

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類。

但是,代碼中的任何地方都使用對象選項。爲什麼?

爲什麼在加載位圖之前使用optionss對象配置格式時,如果有請求獲取格式?

我很困惑。感謝您的幫助。

回答

0

我不知道你的代碼是來自但是如果你想(我懷疑你)要使用Options對象,那麼你應該改變你的bitmap = BitmapFactory.decodeStream(in);使用它,像這樣:bitmap = BitmapFactory.decodeStream(in, null, options);

+0

此代碼取自一本書,因此我對這些不必要的行感到困惑。我問自己爲什麼作者沒有使用Bitmap decodeFile(String pathName,Options opts)方法?如果他使用了這種方法,代碼會縮短20行,不是嗎? –