2012-02-29 31 views
1

其實我從資產文件夾中打開PNG文件與此代碼:如何用RGB_565創建位圖?

public static Bitmap loadImage(String imageName){ 
    if(imageName.charAt(0) == '/') { 
     imageName = imageName.substring(1); 
    } 
    imageName = imageName + ".png"; 
    Bitmap image = BitmapFactory.decodeStream(getResourceAsStream(imageName)); 
    return image; 
} 
public static InputStream getResourceAsStream(String resourceName) { 
    if(resourceName.charAt(0) == '/') { 
     resourceName = resourceName.substring(1); 
    } 

    InputStream is = null; 
    try { 
     is = context.getAssets().open(resourceName); 
    } catch (IOException e) {e.printStackTrace();} 
    return is; 
} 

這段代碼打開了位圖全cuality,它需要大量的時間來打開它。我將嘗試使用RGB_565來加速位圖的打開。

我該如何改變我的代碼來打開RGB_565位圖?正如你所看到的,我不知道圖像的寬度和高度。

而且任何sugerences加快位圖的開幕式將是受歡迎的

感謝

回答

6

添加BitmapFactory.Options向decodeStream()電話:

BitmapFactory.Options bitmapLoadingOptions = new BitmapFactory.Options(); 
bitmapLoadingOptions.inPreferredConfig = Bitmap.Config.RGB_565; 
BitmapFactory.decodeStream(instream,null,bitmapLoadingOptions); 

至於如何加快加載圖片?不知道你能做什麼,除非可能的話縮小圖像的大小。

0

此代碼爲我工作

AssetManager assetManager = getAssets(); 
    InputStream istr = null; 
    try { 
     istr = assetManager.open(strName); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    BitmapFactory.Options bitmapLoadingOptions = new BitmapFactory.Options(); 
    bitmapLoadingOptions.inPreferredConfig = Bitmap.Config.RGB_565; 
    Bitmap bitmap = BitmapFactory.decodeStream(istr,null,bitmapLoadingOptions); 


    return bitmap;