我正在使用Glide库将图像加载到循环视图中,大图像每个东西都很好但是当图像尺寸很小时我希望减少图像视图宽度以避免低质量视图,我尝试这些代码行但是它不能一直工作并改变重新加载图片时的所有图像尺寸:
Glide.with(context).load(url).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
((imageHolder) holder).pb.setVisibility(View.INVISIBLE);
final GifDrawable gifDrawable;
if (resource instanceof Animatable) {
gifDrawable = (GifDrawable) resource;
gifDrawable.start();
} else {
gifDrawable = null;
}
int imageViewWidth = RelativeLayout.LayoutParams.MATCH_PARENT;
if (getScreenWidth() > (2.5 * resource.getIntrinsicWidth())) {
imageViewWidth = getScreenWidth() / 2;
} else {
}
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(imageViewWidth, RelativeLayout.LayoutParams.WRAP_CONTENT);
((imageHolder) holder).imageView.setLayoutParams(lp);
return false;
}
}).apply(new RequestOptions().placeholder(R.drawable.s).centerInside()).into(((imageHolder) holder).imageView);
这是我的xml:
<ImageView
android:id="@+id/p_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
/>
我不得不说没有下载就无法获得图像大小。
你试过.centerCrop()吗?
使用centerCrop更改centerInside