如何加快在Android上打开.png位图的速度?

时间:2012-02-29 13:12:28

标签: android

欢迎所有

实际上我正在使用以下代码打开assets文件夹中的png文件:

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;
}

此代码打开具有完整质量的位图,并且需要花费大量时间才能打开它。

也欢迎任何加速打开位图的意外

提前致谢

1 个答案:

答案 0 :(得分:0)

您可以缩小图像大小,设置图像从文件加载时的子采样方式:

BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inSampleSize= 2; // When 2, the orignal width will be divided by 2, and the height too.
Bitmap bmSource = BitmapFactory.decodeFile(path, opt);

如果需要从InputStream加载,请执行以下操作:

BitmapFactory.decodeStream(inputStream, null, opt);