2010-12-16 6 views
1

나는 아직까지는 아무런 해결책이없이 오래 동안 이것에 대해 망설였습니다. asynctask를 통해 listview 이미지를 채우고 있지만 채우기 측면에서 정상적으로 작동하지만 목록을 스크롤하면 이미지가 두 번째 또는 두 번째로 사라지는 현상이 다시 발생합니다. 모든 이미지는 내 SD 카드에 저장되며 아무 것도 다운로드되지 않습니다. 방금 Android 개발자 블로그에서 코드를 수정하고 이름을 변경하지 않았습니다. 내의 getView의 관련 부분은 다음과 같습니다안드로이드리스트 뷰 이미지 뷰잉 좌절

이미지 = 이미지 뷰

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

은 다음과 같습니다 처리하는 클래스 보유하고 이미지 파일 image_main = 이미지 뷰 image_table = 표에 PATH :

공용 클래스 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; 
} 

}

답변

관련 문제