图片随机加载与滑翔和SimpleTarget查看传呼机 [英] Images loading randomly into view pager with Glide and SimpleTarget

查看:665
本文介绍了图片随机加载与滑翔和SimpleTarget查看传呼机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用滑翔图像加载到 ViewPager 使用 PagerAdapter
当我使用下面的方法加载图像:

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

一切工作正常,但现在我需要从滑行得到位图,它在加载时为未来的编辑存储在一个地图,所以我改用这种方法为以下之一:

<$p$p><$c$c>Glide.with(mContext).load(mImage).asBitmap().placeholder(R.drawable.placeholder).into(new SimpleTarget&LT;&位图GT;(){                @覆盖
                公共无效onLoadStarted(可绘制占位符){
                    super.onLoadStarted(占位符);
                    mImageView.setImageDrawable(占位符);
                }                @覆盖
                公共无效onResourceReady(位图位图,GlideAnimation&LT ;?超级位图&GT; glideAnimation){
                    如果(位图!= NULL){
                        mImageView.setImageBitmap(位图);
                    }
                    mBitmapMap.put(位置,位图);
                    mInterface.onImageLoaded(位置,位图);
                }
            });

但结果是,该图像不总是显示。我认为这是一些如何与事实滑行加载图像异步并在某些时候它返回时, instatiateItem 法已经运行完毕。

它看起来像这样的问题是相关的:
<一href=\"http://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view\">ViewPager PagerAdapter更新不及时的查看

但建议没有帮助了我。

是否有人遇到了这个问题,有一个解决方案呢?

先谢谢了。


解决方案

有关此问题的解决方案是使用另一种目标,而不是使用 SimpleTarget 对象,我使用的时候,我写我和我的猜测图像处理异步更好 BitmapImageViewTarget 对象取代了它的问题。所以最终code,我用的是:

<$p$p><$c$c>Glide.with(BaseApplication.getInstance()).load(newContent).asBitmap().placeholder(R.drawable.ic_action_picture).into(new BitmapImageViewTarget(mIvContent){
                    @覆盖
                    公共无效onLoadStarted(可绘制占位符){
                        super.onLoadStarted(占位符);
                        mIvContent.setImageDrawable(占位符);
                    }                    @覆盖
                    公共无效onResourceReady(位图资源,GlideAnimation&LT ;?超级位图&GT; glideAnimation){
                        super.onResourceReady(资源,glideAnimation);
                        mBitmapMap.put(位置,资源);
                        progressBar.setVisibility(View.INVISIBLE);
                        mIvContent.setImageBitmap(资源);
                    }                    @覆盖
                    公共无效onLoadFailed(例外五,可绘制errorDrawable){
                        super.onLoadFailed(即errorDrawable);
                        progressBar.setVisibility(View.INVISIBLE);
                    }
                });

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: ViewPager PagerAdapter not updating the View

But the suggestions there did not helped me.

Does someone encountered this problem and has a solution for it?

Thanks in advance.

解决方案

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

这篇关于图片随机加载与滑翔和SimpleTarget查看传呼机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆