平常设置圆角
public static void setRoundedCornersImage(Context context, String url, ImageView imageView, int roundingRadius, int errorImage, int placeholderImage) {
RequestOptions options = new RequestOptions()
.centerCrop()
.placeholder(placeholderImage) //预加载图片
.error(errorImage) //加载失败图片
.bitmapTransform(new RoundedCorners(DataTools.dip2px(context, roundingRadius))); //圆角
Glide.with(context).load(url).apply(options).into(imageView);
}
这样设置会导致 .centerCrop() 方法无效(圆角和它是同一类型设置),如果你在xml也设置了这个则会出现圆角无效的问题(大抵是因为设置圆角后又被剪切了的原因吧)
如果想要既占满图片大小,又显示圆角,则需要自定义一些这个圆角控件(RoundedCorners)
进入源码,找到 transform()方法,修改(可以全复制出来修改)
private boolean isCenterCrop;
/**
* @param roundingRadius the corner radius (in device-specific pixels).
* @throws IllegalArgumentException if rounding radius is 0 or less.
*/
public RoundedCorners2(int roundingRadius, boolean isCenterCrop) {
Preconditions.checkArgument(roundingRadius > 0, "roundingRadius must be greater than 0.");
this.roundingRadius = roundingRadius;
this.isCenterCrop = isCenterCrop;
}
@Override
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
if (isCenterCrop) {
Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
return TransformationUtils.roundedCorners(pool, bitmap, roundingRadius);
} else {
return TransformationUtils.roundedCorners(pool, toTransform, roundingRadius);
}
}
增加了一个字段:isCenterCrop 用来设置是否需要另设置 centerCrop效果,然后在 transform 里设置
调用
public static void setRoundedCornersImage(Context context, String url, ImageView imageView, int roundedCorners,boolean isCenterCrop) {
RequestOptions options = new RequestOptions()
.bitmapTransform(new RoundedCorners2(DataTools.dip2px(context, roundedCorners), isCenterCrop)); //圆角
Glide.with(context).load(url).apply(options).into(imageView);
}
如果想要设置占位图或者失败图的话就会发现,这俩不是圆角,很不合群,这里需要再给它俩也设置下,使用 .thumbnail()方法
public static void setRoundedCornersImage(Context context, String url, ImageView imageView, int roundedCorners,boolean isCenterCrop, int errorImage, int placeholderImage) {
RequestOptions options = new RequestOptions()
.placeholder(placeholderImage) //预加载图片
.error(errorImage) //加载失败图片
.bitmapTransform(new RoundedCorners2(DataTools.dip2px(context, roundedCorners), isCenterCrop)); //圆角
Glide.with(context).load(url).apply(options)
.thumbnail(loadTransform(imageView.getContext(), placeholderImage, roundedCorners, isCenterCrop))
.thumbnail(loadTransform(imageView.getContext(), errorImage, roundedCorners, isCenterCrop))
.into(imageView);
}
private static RequestBuilder<Drawable> loadTransform(Context context, @DrawableRes int placeholderId, int roundedCorners, boolean isCenterCrop) {
RequestOptions options = new RequestOptions()
.bitmapTransform(new RoundedCorners2(DataTools.dip2px(context, roundedCorners), isCenterCrop));
return Glide.with(context)
.load(placeholderId)
.apply(options);
}
/**
* dipתΪ px
*/
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}