1

我从服务获取图像 url 路径,我必须在我的列表视图中显示(它还必须显示我从服务获取的其他文本)。这是从 url 路径下载图像的代码(我从 http获得://android-developers.blogspot.in/2010/07/multithreading-for-performance.html)和我在自定义适配器中获取视图():

公共类 ImageDownloader {

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

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

    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();
                BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView);
                // Change bitmap only if this process is still associated with it
                if (this == bitmapDownloaderTask) {
                    imageView.setImageBitmap(bitmap);
                }
            }
        }
    }

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

    static class DownloadedDrawable extends ColorDrawable {
        private final WeakReference<BitmapDownloaderTask> bitmapDownloaderTaskReference;

        public DownloadedDrawable(BitmapDownloaderTask bitmapDownloaderTask) {
            super(Color.WHITE);
            bitmapDownloaderTaskReference =
                new WeakReference<BitmapDownloaderTask>(bitmapDownloaderTask);
        }

        public BitmapDownloaderTask getBitmapDownloaderTask() {
            return bitmapDownloaderTaskReference.get();
        }
    }

    public static Bitmap downloadBitmap(String src) {
         try {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

            StrictMode.setThreadPolicy(policy); 
            // Log.e("src",src);
             URL url = new URL(src);
             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
             connection.setDoInput(true);
             connection.connect();
             InputStream input = connection.getInputStream();
             Bitmap myBitmap = BitmapFactory.decodeStream(input);
            // Log.e("Bitmap","returned");
             return myBitmap;
         } catch (IOException e) {
             e.printStackTrace();
             //Log.e("Exception",e.getMessage());
             return null;
         }

}

    @Override
    public View getView(final int position, View view, ViewGroup arg2) {
        // TODO Auto-generated method stub

        if (view == null) {

                view = inflater.inflate(R.layout.hospital_row,                                     null);

                holder = new MyViewHolder();

                holder.hospName = (TextView) view
                        .findViewById(R.id.hospname);

                holder.hospImage = (ImageView) view
                        .findViewById(R.id.hospitalicon);


                view.setTag(holder);
            }
            holder = (MyViewHolder) view.getTag();

            values = valueList.get(position);

            // for image
            path = values.getImagePath();
            ImageDownloader idm=new ImageDownloader();
            idm.download(path, holder.hospImage);


            holder.hospName.setText(values.getHospitalName());
            }

但是我面临的问题是每次滚动时,都会调用 get view() 并且从异步任务中重新加载整个图像列表。所以有没有办法在我滚动时不重新加载或刷新整个列表.. . 注意:我不想使用 LRUCache(或任何其他内存缓存)来缓存它们,因为有很多图像(大约 1000 个)会消耗大量内存。

4

1 回答 1

0

请从 getView 中删除此代码。

ImageDownloader idm=new ImageDownloader(); idm.download(路径,holder.hospImage);

并且所有图像都存储在 ArrayList 和 getView 中的 arraylist 数据集中。

于 2013-07-08T09:15:14.240 回答