我在用着Glide
将图像加载到ViewPager
使用一个PagerAdapter
。
当我使用以下方法加载图像时:
Glide.with(mContext).load(mImage).placeholder(R.drawable.placeholder).into(mImageView);
一切正常,但现在我需要从 glide 获取位图,并在加载时将其存储在地图中以供将来编辑,因此我将此方法切换为以下方法:
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);
}
});
但结果是图像并不总是显示出来。我认为这与滑行异步加载图像这一事实有一定的关系,并且在某些时候它会返回instatiateItem
方法已经运行完毕。
看起来像这个问题 https://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view相关的。但那里的建议对我没有帮助。有人遇到过这个问题并有解决方案吗?
这个问题的解决方案是使用另一种目标,而不是使用SimpleTarget
我在写问题时使用的对象我将其替换为BitmapImageViewTarget
我认为该对象可以更好地异步处理图像。所以我使用的最终代码是:
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);
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)