Gridview图像混合在滚动上

时间:2016-08-22 23:53:44

标签: android gridview

我试图在滚动时解决有关gridview中混合图像的问题。我已经在这里看到了关于这个问题的类似帖子,但不幸的是我还没有解决它。

我使用asynctask在gridView中加载图像,我在网格中只有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;
    }

2 个答案:

答案 0 :(得分:1)

问题背后的原因是BitmapWorkerTask没有通过启动顺序完成,因此它可以覆盖旧版本的最新结果。

为了避免这种情况,您必须为每个视图保留一个BitmapWorkerTask,如果它仍在工作则取消旧视图,这样它就不会覆盖新BitmapWorkerTask的结果

您可以使用setTaggetTag方法来实现此目的,并保留对每个视图的最新BitmapWorkerTask的引用。

答案 1 :(得分:0)

在启动图像加载线程时,您没有将Tag设置为ImageView,当线程完成作业时,为什么将图像设置为屏幕上的当前显示。 你应该在 BitmapWorkerTask 类中传递一些id作为参数。