簡體   English   中英

Android ListView ImageView沮喪

[英]Android listview imageview frustration

我已經為此困擾了很長時間,但仍然沒有解決方案。 我正在通過asynctask填充listview圖像,並且在填充方面效果很好,但是問題是,當我滾動列表時,圖像消失了一兩秒,然后重新出現。 請注意,所有圖像都在我的SD卡上,沒有下載任何內容。 我只是調整了Android開發者博客中的代碼,而從未重命名它。 我的getView的相關部分如下所示:

image =圖像文件的路徑image_main = imageView image_table =保存imageView的表

                list_image.setVisibility(View.VISIBLE);                 
                ImageDownloader imgDwn = new ImageDownloader();
                imgDwn.download(image, image_main, image_table);
            } else {
                list_image.setVisibility(View.GONE);
                image_table.setVisibility(View.GONE);
                image_main.setImageBitmap(null);
            }

處理它的類如下所示:

公共類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;
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM