Images loading randomly into view pager with Glide and SimpleTarget

邮差的信 提交于 2020-01-14 10:36:09

问题


I'm using Glide to load image into a ViewPager using an PagerAdapter. When I load the images using the following method:

Glide.with(mContext).load(mImage).placeholder(R.drawable.placeholder).into(mImageView);

Everything works fine, but now I need to get the bitmap from glide and store it in a map when it loads for future edit, so I switched this method to the following one:

Glide.with(mContext).load(mImage).asBitmap().placeholder(R.drawable.placeholder).into(new SimpleTarget<Bitmap>() {

                @Override
                public void onLoadStarted(Drawable placeholder) {
                    super.onLoadStarted(placeholder);
                    mImageView.setImageDrawable(placeholder);
                }

                @Override
                public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
                    if (bitmap != null) {
                        mImageView.setImageBitmap(bitmap);
                    }
                    mBitmapMap.put(position, bitmap);
                    mInterface.onImageLoaded(position, bitmap);
                }
            });

But the result is that the image not always shown. I think it's some how related to the fact the glide loads images async and at some point it returns when instatiateItem method already finished running.

It looks like this question is related. But the suggestions there did not help me. Has someone encountered this problem and has a solution for it?


回答1:


The solution for this issue was to use another kind of target, instead of using the SimpleTarget object which I was using when I wrote the question I replaced it with the BitmapImageViewTarget object which I guess handles images asynchronously better. So the final code that I used for it is:

Glide.with(BaseApplication.getInstance()).load(newContent).asBitmap().placeholder(R.drawable.ic_action_picture).into(new BitmapImageViewTarget(mIvContent) {
                    @Override
                    public void onLoadStarted(Drawable placeholder) {
                        super.onLoadStarted(placeholder);
                        mIvContent.setImageDrawable(placeholder);
                    }

                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        super.onResourceReady(resource, glideAnimation);
                        mBitmapMap.put(position, resource);
                        progressBar.setVisibility(View.INVISIBLE);
                        mIvContent.setImageBitmap(resource);
                    }

                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        super.onLoadFailed(e, errorDrawable);
                        progressBar.setVisibility(View.INVISIBLE);
                    }
                });


来源:https://stackoverflow.com/questions/34336505/images-loading-randomly-into-view-pager-with-glide-and-simpletarget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!