码迷,mamicode.com
首页 > 其他好文 > 详细

listView异步加载图片

时间:2015-02-05 16:33:34      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:android   listview   异步   图片   bitmap   

下载图片方法:
static Bitmap downloadBitmap(String url) {
   
final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
   
final HttpGet getRequest = new HttpGet(url);

   
try {
       
HttpResponse response = client.execute(getRequest);
       
final int statusCode = response.getStatusLine().getStatusCode();
       
if (statusCode != HttpStatus.SC_OK) {
           
Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url);
           
return null;
       
}
       
       
final HttpEntity entity = response.getEntity();
       
if (entity != null) {
           
InputStream inputStream = null;
           
try {
                inputStream
= entity.getContent();
               
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
               
return bitmap;
           
} finally {
               
if (inputStream != null) {
                    inputStream
.close();  
               
}
                entity
.consumeContent();
           
}
       
}
   
} catch (Exception e) {
       
// Could provide a more explicit error message for IOException or IllegalStateException
        getRequest
.abort();
       
Log.w("ImageDownloader", "Error while retrieving bitmap from " + url, e.toString());
   
} finally {
       
if (client != null) {
            client
.close();
       
}
   
}
   
return null;
}
注意:早起版本的BitmapFactory.decodeStream()方法有一个bug,可能会影响上面代码的执行。可以自己实现一个FlushedInputStream(inputStream)来解决这个问题。下面是这个帮助类的实现:
static class FlushedInputStream extends FilterInputStream {
   
public FlushedInputStream(InputStream inputStream) {
       
super(inputStream);
   
}

   
@Override
   
public long skip(long n) throws IOException {
       
long totalBytesSkipped = 0L;
       
while (totalBytesSkipped < n) {
           
long bytesSkipped = in.skip(n - totalBytesSkipped);
           
if (bytesSkipped == 0L) {
                 
int byte = read();
                 
if (byte < 0) {
                     
break;  // we reached EOF
                 
} else {
                      bytesSkipped
= 1; // we read one byte
                 
}
           
}
            totalBytesSkipped
+= bytesSkipped;
       
}
       
return totalBytesSkipped;
   
}
}
这个方法保证skip()方法跳过了提供好的字节个数,除非已经到了文件的末尾。

如果直接把downloadBitmap方法在ListView的adapter里使用可能会导致滑动卡顿。因为每次getView()执行的时候都会等待图片的加载,会影响UI的渲染。

异步任务初步介绍
技术分享
下面是BitmapDownloaderTask的实现:
class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
   
private String url;
   
private final WeakReference<ImageView> imageViewReference;

   
public BitmapDownloaderTask(ImageView imageView) {
        imageViewReference
= new WeakReference<ImageView>(imageView);
   
}

   
@Override
   
// Actual download method, run in the task thread
   
protected Bitmap doInBackground(String... params) {
         
// params comes from the execute() call: params[0] is the url.
         
return downloadBitmap(params[0]);
   
}

   
@Override
   
// Once the image is downloaded, associates it to the imageView
   
protected void onPostExecute(Bitmap bitmap) {
       
if (isCancelled()) {
            bitmap
= null;
       
}

       
if (imageViewReference != null) {
           
ImageView imageView = imageViewReference.get();
           
if (imageView != null) {
                imageView
.setImageBitmap(bitmap);
           
}
       
}
   
}
}
doInBackground()方法是在它自己的独立线程中运行的。
onPostExecute()方法是运行在调用者所在的UI线程中的。注意ImageView使用的是WeakReference,这样就能保证当下载正在进行的时候不会影响Imageview的回收。比如当正在下载的时候用户关掉了activity,这是ImageView可能会被回收。所以要在onPostExecute方法中检查imageview的弱引用和iamgeview是否都为空。

listView异步加载图片

标签:android   listview   异步   图片   bitmap   

原文地址:http://blog.csdn.net/kent_todo/article/details/43528117

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!