两种方案:
1、重写BitmapTransformation
重写方法解决冲突,来自CSDN的一个解决方案
public class GlideRoundTransform extends BitmapTransformation {
private static float radius = 0f;
public GlideRoundTransform(Context context) {
this(context, 4);
}
public GlideRoundTransform(Context context, int dp) {
super(context);
this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
return roundCrop(pool, bitmap);
}
private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
canvas.drawRoundRect(rectF, radius, radius, paint);
return result;
}
public String getId() {
return getClass().getName() + Math.round(radius);
}
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
}
}
在加载的时候使用
RequestOptions myOptions = new RequestOptions()
.transform(new GlideRoundTransform(this,30));
Glide.with(this)
.load(R.drawable.item1)
.apply(myOptions)
.into(icon1);
地址http://blog.csdn.net/flyinbed_/article/details/75506062
2、使用第三方框架
库地址:
https://github.com/wasabeef/glide-transformations
该库是专门针对glide的一个辅助类,包括裁剪
效果图
引入方法
compile 'jp.wasabeef:glide-transformations:3.0.1'
使用方法
Glide.with(mContext)
.load(aClass.img)
.apply(RequestOptions.bitmapTransform(new MultiTransformation(
new CenterCrop(),
new RoundedCornersTransformation(SizeUtils.dp2px(5), 0, RoundedCornersTransformation.CornerType.TOP))))
.into(imageView);
这里RoundedCornersTransformation的第一个参数需要转换成像素