zoukankan      html  css  js  c++  java
  • 异步加载图片

    关键搜索词:AsyncTask,WeakReference,SoftReference,LinkedHashMap,ConcurrentHashMap

    从网络下载一张图片是很简单的,运用HTTP类就可以完成,如下例子:

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

    而如果图片多了,主线程不能一直等待图片下载全部完成,故异步加载就极为重要。

    异步加载:AsyncTask可以轻松使用UI线程,能够允许在后台操作,然后将结果返回到UI线程上

    而不需要使用复杂的线程或者Handlers

    AsyncTask官网使用4步骤:

     1.onPreExecute() 在UI线程上调用之前执行任务。

      2.doINBackground(Params...) 当OnPreExecute完成执行后此方法在后台执行。返回结果必须返回到这一步,最后被传递到最后一步。

      3.onProgressUpdate(Progress...) 当publishProgress(Progress...)执行之后在UI线程上

    执行。当后台仍然在下载时,这个方法被用来显示任何progress在用户界面上。

    4.onPostExecute(Result) 当后台程序执行结束才执行,后台程序的结果将进入这一步。

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

    并发操作:

    static class DownloadedDrawable extends ColorDrawable {
        private final WeakReference<BitmapDownloaderTask> bitmapDownloaderTaskReference;
    
        public DownloadedDrawable(BitmapDownloaderTask bitmapDownloaderTask) {
            super(Color.BLACK);
            bitmapDownloaderTaskReference =
                new WeakReference<BitmapDownloaderTask>(bitmapDownloaderTask);
        }
    
        public BitmapDownloaderTask getBitmapDownloaderTask() {
            return bitmapDownloaderTaskReference.get();
        }
    }
    public void download(String url, ImageView imageView) {
         if (cancelPotentialDownload(url, imageView)) {
             BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
             DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task);
             imageView.setImageDrawable(downloadedDrawable);
             task.execute(url, cookie);
         }
    }
    private static boolean cancelPotentialDownload(String url, ImageView imageView) {
        BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView);
    
        if (bitmapDownloaderTask != null) {
            String bitmapUrl = bitmapDownloaderTask.url;
            if ((bitmapUrl == null) || (!bitmapUrl.equals(url))) {
                bitmapDownloaderTask.cancel(true);
            } else {
                // The same URL is already being downloaded.
                return false;
            }
        }
        return true;
    }
    private static BitmapDownloaderTask getBitmapDownloaderTask(ImageView imageView) {
        if (imageView != null) {
            Drawable drawable = imageView.getDrawable();
            if (drawable instanceof DownloadedDrawable) {
                DownloadedDrawable downloadedDrawable = (DownloadedDrawable)drawable;
                return downloadedDrawable.getBitmapDownloaderTask();
            }
        }
        return null;
    }

    原文:http://android-developers.blogspot.jp/2010/07/multithreading-for-performance.html  

    谢谢

  • 相关阅读:
    通过Get-Group导出组的成员
    VNC Server (CentOS 7 GNOME)
    VNC Server (Ubuntu 16.04.3 GNOME)
    输入输出重定向
    Linux下的网卡Bonding
    硬件性能测试
    Linux里的稀疏文件
    Linux下CPU信息的查看
    工作中常用到的Linux命令
    Putty+Xming实现在Windows客户端显示Linux服务器端的图形化程序
  • 原文地址:https://www.cnblogs.com/gfqFighting/p/2845721.html
Copyright © 2011-2022 走看看