注意事项:
由于Glide缓存的梗,如果同一张地址的头像多处使用并且大小不一样,必要的设置如下:
skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.SOURCE)
使用BitmapTransformation变换 ,此类由网上所找的代码修改,修正了原版的bug
public class GlideCircleTransform extends BitmapTransformation {
private Paint mBorderPaint; private float mBorderWidth; private Context context; public GlideCircleTransform(Context context) { super(context); this.context = context; } /** * @param context * @param borderWidth 头像圆环的宽度 * @param borderColor 头像圆环的颜色 */ public GlideCircleTransform(Context context, int borderWidth, int borderColor) { super(context); mBorderWidth = DensityUtil.dip2px(context, borderWidth); mBorderPaint = new Paint(); mBorderPaint.setDither(true); mBorderPaint.setAntiAlias(true); mBorderPaint.setColor(borderColor); mBorderPaint.setStyle(Paint.Style.STROKE); mBorderPaint.setStrokeWidth(borderWidth); this.context = context; } protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { System.out.println("#transform:" + outWidth + " " + outHeight); return circleCrop(pool, toTransform, outWidth, outHeight); } private Bitmap circleCrop(BitmapPool pool, Bitmap source, int outWidth, int outHeight) { if (source == null) return null; int sourceWidth = source.getWidth(); int sourceHeight = source.getHeight(); int outSize = Math.min(outWidth, outHeight); int size = (int) (Math.min(outWidth, outHeight) - mBorderWidth * 2); int sourceSize = (int) (Math.min(sourceWidth, sourceHeight) - mBorderWidth * 2); int x = (sourceWidth - sourceSize) / 2; int y = (sourceHeight - sourceSize) / 2; // 创建操作图片用的matrix对象 Matrix matrix = new Matrix(); //计算缩放率,新尺寸除原始尺寸 float scaleWidth = ((float) size) / sourceSize; float scaleHeight = ((float) size) / sourceSize; // 缩放图片动作 matrix.postScale(scaleWidth, scaleHeight); // 必要的缩放保证fill输出图片最大宽高 Bitmap squared = Bitmap.createBitmap(source, x, y, sourceSize, sourceSize, matrix, true); Bitmap result = pool.get(outSize, outSize, Bitmap.Config.ARGB_8888); if (result == null) { result = Bitmap.createBitmap(outSize, outSize, Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(result); Paint paint = new Paint(); paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); paint.setAntiAlias(true); float r = outSize / 2f; canvas.drawCircle(r, r, r - mBorderWidth / 2, paint); if (mBorderPaint != null) { float borderRadius = r; canvas.drawCircle(r, r, borderRadius - mBorderWidth / 2, mBorderPaint); } System.out.println("#circleCrop"); return result; } @Override public String getId() { return ""; } }
调用代码如下:
Glide.with(context).load(url).asBitmap().centerCrop().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(Global.HEAD_DEFAULT).error(Global.HEAD_DEFAULT).fallback(Global.HEAD_DEFAULT).transform(createCircleTransform(context, borderWidth, borderColor))另外还有一种方法,如果要支持带颜色的圆环头像建议上一种:
Glide.with(context).load(url).asBitmap().centerCrop().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(Global.HEAD_DEFAULT).error(Global.HEAD_DEFAULT).fallback(Global.HEAD_DEFAULT).into(new BitmapImageViewTarget(imageView) { @Override protected void setResource(Bitmap resource) { RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), resource); circularBitmapDrawable.setCircular(true); imageView.setImageDrawable(circularBitmapDrawable); } });