BGABanner实现图片自适应屏幕宽度且保持原有图片比例-方法1


@Override
public void fillBannerItem(final BGABanner banner, final ImageView itemView, String model, int position) {
Glide.with(context)
.load(model)
.placeholder(R.mipmap.banner_holder)
.listener(new RequestListener() {
@Override
public boolean onException(Exception e, String model, Target target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target target, boolean isFromMemoryCache, boolean isFirstResource) {
if (itemView == null) {
return false;
}
if (itemView.getScaleType() != ImageView.ScaleType.FIT_XY) {
itemView.setScaleType(ImageView.ScaleType.FIT_XY);
}
ViewGroup.LayoutParams params = itemView.getLayoutParams();

int vw = itemView.getWidth() - itemView.getPaddingLeft() - itemView.getPaddingRight();
float scale = (float) vw / (float) resource.getIntrinsicWidth();
int vh = Math.round(resource.getIntrinsicHeight() * scale);
params.height = vh + itemView.getPaddingTop() + itemView.getPaddingBottom();
System.out.println("image height:" + params.height);
System.out.println("image width:" + params.width);
itemView.setLayoutParams(params);
ViewGroup.LayoutParams banner_params = banner.getLayoutParams();
banner_params.width = params.width;
banner_params.height = params.height;
banner.setLayoutParams(banner_params);
return false;
}
})
.into(itemView);
}

上面使用了读取网络图片尺寸的方式实现,缺点就是还没加载完图片时BGABanner的尺寸还没有自适应。

第二个方式:

你可能感兴趣的:(BGABanner实现图片自适应屏幕宽度且保持原有图片比例-方法1)