2012-02-29 34 views
2

欢迎各界如何加速在Android上打开.png位图?

其实我从资产文件夹中打开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; 
} 

这段代码打开全cuality位图,它需要大量的时间来打开它。

而且任何sugerences加快位图的开幕式将是受欢迎的

在此先感谢

回答

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); 
+0

如果我这样做,我的位图有原始PNG文件的一半大小?我需要该位图具有相同的尺寸...请告诉我, – NullPointerException 2012-02-29 13:35:50

+0

@ AndroidUser99是的,一半大小。你的形象有多大?当我使用decodeFile的方法总是很快,即使我不减少图像。 – Derzu 2012-02-29 13:39:19

+0

@ AndroidUser99您也可以尝试在真正需要它之前预先加载图像。它可以在另一个线程中完成。 – Derzu 2012-02-29 13:56:03