方法一
Glide.with(Context)
.load("图片资源")
.apply(RequestOptions.bitmapTransform(new CircleCrop()))//圆形
.into(editinformationImage);
方法二
封装类 自己进行裁剪
/**
* glide圆角
*/
public class GlideRoundTransform extends BitmapTransformation {
private static float radius = 0f;
private static float Corners=0;
public GlideRoundTransform() {
this(4,0);
}
public GlideRoundTransform(int dp,int Corners) {
super();
this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
this.Corners=Corners;
}
@Override
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
//变换的时候裁切
Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
return roundCrop(pool, bitmap);
}
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
}
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);
//0f, 0f, source.getWidth(), source.getHeight()
RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
canvas.drawRoundRect(rectF, radius, radius, paint);
//4个角 0,100f,source.getWidth(), source.getHeight()
RectF rectRound = new RectF(Corners, Corners,Corners,Corners );
canvas.drawRect(rectRound, paint);
return result;
}
}
使用
RequestOptions options = new RequestOptions()
.centerCrop()
.placeholder(R.mipmap.ic_launcher_round) //预加载图片
.error(R.drawable.ic_launcher_foreground) //加载失败图片
.priority(Priority.HIGH) //优先级
.diskCacheStrategy(DiskCacheStrategy.NONE) //缓存
.transform(new GlideRoundTransform(20,20)); //圆角
Glide.with(Context).load("图片路径").apply(options).into(viewHolder.image);