依赖
implementation 'com.github.bumptech.glide:glide:4.9.0'
代码
Glide.with(this).asBitmap()
.load(newPath)
.apply(RequestOptions.bitmapTransform(new CircleCrop()))
.transform(new GlideCircleWithBorder(this, 2, Color.parseColor("#ffffff")))
.into(pic);
//设置厚为圆形边厚度自定义
package com.example.headportrait;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import java.security.MessageDigest;
/**
* Copyright:why
* Created by Baron on 2020/10/23 12:43 PM
* Emaile:wanghongyuwh@163.com
* Description:
* Modify time:
* Modified by:
*/
public class GlideCircleWithBorder extends BitmapTransformation {
private Paint mBorderPaint;
private float mBorderWidth;
public GlideCircleWithBorder(Context context) {
super();
}
public GlideCircleWithBorder(Context context, int borderWidth, int borderColor) {
super();
mBorderWidth = Resources.getSystem().getDisplayMetrics().density * borderWidth;
mBorderPaint = new Paint();
mBorderPaint.setDither(true);
mBorderPaint.setAntiAlias(true);
mBorderPaint.setColor(borderColor);
mBorderPaint.setStyle(Paint.Style.STROKE);
mBorderPaint.setStrokeWidth(mBorderWidth);
}
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return circleCrop(pool, toTransform);
}
private Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if (source == null) {
return null;
}
int size = (int) (Math.min(source.getWidth(), source.getHeight()) - (mBorderWidth / 2));
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(size, size, 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 = size / 2f;
canvas.drawCircle(r, r, r, paint);
if (mBorderPaint != null) {
float borderRadius = r - mBorderWidth / 2;
canvas.drawCircle(r, r, borderRadius, mBorderPaint);
}
return result;
}
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
}
}
然后如果不设置圆形这里是圆角的设置方法
RoundedCorners roundedCorners=new RoundedCorners(10);
RequestOptions requestOptions = RequestOptions.bitmapTransform(roundedCorners).override(300,300);
Glide.with(context).load(list.get(i).getMasterPic()).apply(requestOptions).into(holder.ImageView);