2010-12-12 113 views
7

當滾動我的listview與圖像。圖像將消失,然後在一兩秒後重新出現。任何關於此事的幫助將不勝感激!滾動時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; 
    } 
} 

回答

0

那麼保羅至於你的代碼而言,你是從所需的每次下載圖像服務器,每當你滾動你的列表視圖。一個位圖緩存實現可能可以解決您的問題。

+1

其實我的圖片在SD卡上。我只是測試了我在Android開發人員博客上找到的這些代碼,並從未困擾過更改名稱。 – Paul 2010-12-12 16:58:47

4

我通過閱讀question來發現問題。如果你隱藏了一些元素,那麼你必須再次展示它們:)簡單。