1

在我的 ListView 中,每一行都有一个 ImageView 和两个 TextView。ListView 的图片是从我的 Galaxy Nexus 的内存中加载的。我已经将它缩小到 100x100 使用

Bitmap scaled = Bitmap.createScaledBitmap(de, 80, 80, true);

但它仍然需要几秒钟才能加载。

我能做些什么?

编辑:

public Bitmap resizeBitmap(String path){
    BitmapFactory.Options options = new BitmapFactory.Options();
    InputStream is = null;
    try {
        is = new FileInputStream(path);
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    BitmapFactory.decodeStream(is,null,options);
    try {
        is.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        is = new FileInputStream(path);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // here w and h are the desired width and height
    options.inSampleSize = Math.max(options.outWidth/100, options.outHeight/100);
    // bitmap is the resized bitmap
    Bitmap bitmap = BitmapFactory.decodeStream(is,null,options);
    return bitmap;
}
4

3 回答 3

1

第一点我可以从您的代码中看到您正在对流进行两次解码......第一个被解码的地方甚至没有在任何地方使用......所以删除该语句将提高代码的执行速度。

 //BitmapFactory.decodeStream(is,null,options); comment this line
try {
    is.close();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

另外请问为什么不能使用图片的缩略图?而不是调整每个图像的大小....如果您尝试在应用中显示图像的缩略图

是的,有一个完整的表格存储您需要通过 Cursor api 访问它的图像缩略图,例如:

    Cursor mediaCursor = managedQuery(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null,
            null, null);

从此光标中,您可以获取 imageId,然后借助图像 Id,您可以使用以下MediaStore.Images.Thumbnails.getThumbnail()方法检索图像的缩略图,例如下面的一些代码将有所帮助:

     Bitmap thumbBitmap = null;

    thumbBitmap = MediaStore.Images.Thumbnails.getThumbnail(cr, imageID, MediaStore.Images.Thumbnails.MICRO_KIND, null);
    if(thumbBitmap != null){
        return new BitmapDrawable(thumbBitmap);
    }
于 2013-05-14T11:14:50.430 回答
0

在 ListView 中有效地显示位图是一项相当复杂的任务,涉及到很多变量。我建议您阅读本文并下载他们提供的代码示例。这是一个很好的起点,在简单的情况下可能足以复制粘贴。

于 2013-05-14T10:55:13.937 回答
0

最好的方法是将它们加载到AsyncTask. 在这里,您有一个展示这种方法的精彩视频: https ://developers.google.com/events/io/2012/sessions/gooio2012/103/

于 2013-05-14T11:20:58.873 回答