滚动时,Android列表视图图像消失

时间:2010-12-12 01:22:22

标签: android listview

使用图片滚动列表视图时。图像将消失,然后在一两秒后重新出现。任何有关此事的帮助将不胜感激!

这是在我的getView中调用以下代码:

        image_main.setImageBitmap(null);
        if (curr == 0 && image != null) {

            list_image.setVisibility(View.VISIBLE);
            image_preference = preferences.getString("image_preferences", "false");
            time_right.setVisibility(View.GONE);
            if (image_preference.equals("false")) {  
                 ImageDownloader imgDwn = new ImageDownloader();
                 imgDwn.download(image, image_main, image_table);
            }

我的代码:

public class ImageDownloader {

    public void download(String url, ImageView imageView, TableLayout imageTable) {
        if (cancelPotentialDownload(url, imageView)) {
        BitmapDownloaderTask task = new BitmapDownloaderTask(imageView, imageTable);
        DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task);
        imageView.setImageDrawable(downloadedDrawable);
        task.execute(url);
        }
    }

    class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
        String url;
        private final WeakReference<ImageView> imageViewReference;
        private final WeakReference<TableLayout> imageTableReference;

        public BitmapDownloaderTask(ImageView imageView, TableLayout imageTable) {
            imageViewReference = new WeakReference<ImageView>(imageView);
            imageTableReference = new WeakReference<TableLayout>(imageTable);
        }

          @Override
          protected Bitmap doInBackground(String... params) {
                 BitmapFactory.Options o = new BitmapFactory.Options();
                  o.inJustDecodeBounds = true;
                  BitmapFactory.decodeFile(params[0], o);
                  final int REQUIRED_SIZE=70;

                  //Find the correct scale value. It should be the power of 2.
                  int width_tmp=o.outWidth, height_tmp=o.outHeight;
                  int scale=4;
                  while(true){
                      if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                          break;
                      width_tmp/=2;
                      height_tmp/=2;
                      scale++;
                  }
                  //Decode with inSampleSize
                  BitmapFactory.Options o2 = new BitmapFactory.Options();
                  o2.inSampleSize=scale;       
                  return BitmapFactory.decodeFile(params[0], o2);
          }

          @Override
          protected void onPostExecute(Bitmap result) {
                if (isCancelled()) {
                    result = null;
                }

                if (imageViewReference != null) {
                    ImageView imageView = imageViewReference.get();
                    TableLayout imageTable = imageTableReference.get();
                    BitmapDownloaderTask bitmapDownloaderTask = ImageDownloader.getBitmapDownloaderTask(imageView);
                    // Change bitmap only if this process is still associated with it
                    if (this == bitmapDownloaderTask) {
                          imageView.setImageBitmap(result);
                          imageView.setVisibility(View.VISIBLE);
                          imageTable.setVisibility(View.VISIBLE);
                    }              
                }
            }
    }

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

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

2 个答案:

答案 0 :(得分:4)

我通过阅读question来解决问题。如果你隐藏了一些元素,那么你必须再次显示它们:)这样简单。

答案 1 :(得分:0)

就您的代码而言,只要您在列表视图中滚动,每次都会从所需的服务器下载图像。可能是位图缓存实现可以解决您的问题。