2012-02-29 28 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;