2016-08-22 1 views
3

スクロールするときにグリッドビュー内の画像が混ざっているという問題について、この問題を解決しようとしています。私はすでにこの問題について同様の記事を見ましたが、残念ながら私はまだ解決しませんでした。スクロールでグリッドビューの画像が混在する

私はgridViewで画像を読み込むためにasynctaskを使用し、グリッドにはimageViewしかありません。

ありがとうございました!ここで

は私のコードです:

@Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 

     ImageView img; 
     if (convertView == null) { 
      img = new ImageView(GalleryActivity.this); 
      WindowManager wm = (WindowManager) GalleryActivity.this.getSystemService(Context.WINDOW_SERVICE); 
      Display display = wm.getDefaultDisplay(); 
      Point size = new Point(); 
      display.getSize(size); 
      int width = size.x; 
      width = width/3 - 4; 
      img.setLayoutParams(new AbsListView.LayoutParams(width, width)); 
      img.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     } else { 
      img = (ImageView) convertView; 
     } 


     File dics = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); 
     File dir = new File(dics, "yyy"); 

     if (!dir.exists() && !dir.mkdirs()) { 
      img.setImageResource(holder(position + 1)); 
     } else { 
      String file_name = dir.getAbsolutePath() + "/" + (position + 1) + ".jpg"; 
      if (new File(file_name).exists()) { 
       BitmapWorkerTask task = new BitmapWorkerTask(img); 
       task.execute(file_name); 
       items[position].setStatus(1); 
      } else { 
       img.setImageResource(holder(position + 1)); 
      } 
     } 
     return img; 
    } 


class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> { 
     private final WeakReference<ImageView> imageViewReference; 
     private String data; 

     public BitmapWorkerTask(ImageView imageView) { 
      imageViewReference = new WeakReference<ImageView>(imageView); 
     } 

     // Decode image in background. 
     @Override 
     protected Bitmap doInBackground(String... params) { 
      data = params[0]; 
      return decodeSampledBitmapFromResource(data,100,100); 

     } 

     // Once complete, see if ImageView is still around and set bitmap. 
     @Override 
     protected void onPostExecute(Bitmap bitmap) { 
      if (imageViewReference != null && bitmap != null) { 
       final ImageView imageView = imageViewReference.get(); 
       if (imageView != null) { 
        imageView.setImageBitmap(bitmap); 
       } 
      } 
     } 
    } 

    public Bitmap decodeSampledBitmapFromResource(String resId, 
                 int reqWidth, int reqHeight) { 

     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(resId,options); 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 
     options.inJustDecodeBounds = false; 
     return BitmapFactory.decodeFile(resId); 
    } 

    public int calculateInSampleSize(
      BitmapFactory.Options options, int reqWidth, int reqHeight) { 
     // Raw height and width of image 
     final int height = options.outHeight; 
     final int width = options.outWidth; 
     int inSampleSize = 1; 

     if (height > reqHeight || width > reqWidth) { 

      final int halfHeight = height/2; 
      final int halfWidth = width/2; 

      while ((halfHeight/inSampleSize) >= reqHeight 
        && (halfWidth/inSampleSize) >= reqWidth) { 
       inSampleSize *= 2; 
      } 
     } 

     return inSampleSize; 
    } 
+0

これのすべてを処理する多くの[Android用画像読み込みライブラリ](http://android-arsenal.com/tag/46)を使用してみませんか? – CommonsWare

+0

@CommonsWareそれはこの問題を除いて完璧に動作します。あなたはそれを助けることができますか? –

答えて

1

あなたの問題の背後にある理由はBitmapWorkerTaskが、それは古い1により、最新の結果を上書きすることができるように順序を起動して終了していないということです。まだそれはあなたがachiveしsetTaggetTagメソッドを使用することができ、新たなBitmapWorkerTask

の結果を上書きしていない働いている場合は、ビューごとに1 BitmapWorkerTaskを維持し、古いものをキャンセルする必要があり、これを回避するために

この目的を達成し、各ビューで最新のBitmapWorkerTaskを参照してください。

0

画像読み込みスレッドの開始時にImageViewにタグを設定していないため、スレッドがジョブを完了したときに画面に現在の画像が表示されるのはなぜですか? BitmapWorkerTaskクラスのパラメータとして一部のIDを渡す必要があります。

関連する問題