2013-05-14 95 views
1

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

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

使用lazyloading – mukesh 2013-05-14 11:32:21

回答

0

显示ListView中的位图有效地是一个相当复杂的工作,涉及到相当多的变数。我建议你阅读this并下载他们提供的代码示例。这是一个很好的起点,在简单的情况下可能会被复制粘贴。

+0

我已经试过它与我编辑我的问题的代码。 – user896692 2013-05-14 11:04:32

1

第一点我可以从你的代码中看到,你正在解码流两次......解码它的第一个地方,它甚至不在任何地方使用....因此,删除该语句将提高你的执行速度码。

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

也可以问你为什么不能使用图片的缩略图?而不是调整每个图像....如果你想在你的应用程序来显示图像的缩略图

是的,有一个完整的表存储的图像,你需要通过游标API来访问它例如thumbanil:

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

从这个游标就可以得到图像标识,然后用图片ID的帮助下,你可以通过使用例如下面的MediaStore.Images.Thumbnails.getThumbnail()方法retreive图像的缩略图是一些代码,这将有助于:

 Bitmap thumbBitmap = null; 

    thumbBitmap = MediaStore.Images.Thumbnails.getThumbnail(cr, imageID, MediaStore.Images.Thumbnails.MICRO_KIND, null); 
    if(thumbBitmap != null){ 
     return new BitmapDrawable(thumbBitmap); 
    } 
+0

有没有办法使用缩略图?搜索了很多,但只发现了降低原始位图的可能性 – user896692 2013-05-14 11:18:49

+0

是的,看看我的编辑答案 – 2013-05-14 11:26:53

+0

是“cr”在第二个代码示例mediacursor? – user896692 2013-05-14 12:10:06