背景
recyclerview中,IamgeView的高度根据glide加载图片返回的宽高进行配置,会偶发,glide加载生成的drawable宽高混乱,代码如下:
Glide.with(context.getApplicationContext())
.load(img == null ? t : img)
.apply(options)
.addListener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
if (listener != null) {
listener.onLoadFailed(e, model, target, isFirstResource);
}
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
if (listener != null) {
listener.onResourceReady(resource, model, target, dataSource, isFirstResource);
}
return false;
}
})
.into(imageView);
实际开发中,发现onResourceReady的resource,宽高会偶发错乱,因此,换一种方法实现,内容如下:
Glide.with(context.getApplicationContext())
.asBitmap()
.load(img == null ? t : img)
.apply(options)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
if (listener != null) {
int width = resource.getWidth();
int height = resource.getHeight();
LogUtils.d("onResourceReady url: " + finalImg + " width: " + width + " height: " + height);
listener.ready((width >= height), width, height, resource);
}
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
if (listener != null) {
listener.loadFailed();
}
}
});
这种情况下,bitmap经过实测,它的宽高是准确的,所以目前换成了这种方法实现。
原因是因为recyclerview中,imageview的复用问题,导致了drawable的问题,最后加载完图片也是有问题的。该问题需要结合复用,与ImageView进行思路。
网友评论