我正在使用 Glide
使用 PagerAdapter
将图像加载到 ViewPager
中。
当我使用以下方法加载图像时:
Glide.with(mContext).load(mImage).placeholder(R.drawable.placeholder).into(mImageView);
一切正常,但现在我需要从 glide 获取位图并在它加载以供将来编辑时将其存储在 map 中,因此我将此方法切换为以下方法:
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);
}
});
但结果是图像并不总是显示。我认为这与 glide 异步加载图像以及在 instatiateItem
方法已经完成运行时返回的某个点有一些关系。
看起来像this question相关的。但是那里的建议对我没有帮助。有人遇到过这个问题并有解决方案吗?
最佳答案
这个问题的解决方案是使用另一种目标,而不是使用我在写问题时使用的 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);
}
});
关于android - 使用 Glide 和 SimpleTarget 将图像随机加载到 View 寻呼机中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34336505/