glide加载圆形图片待边框

来源:互联网 发布:mysql自动增长id 编辑:程序博客网 时间:2024/05/17 20:35
对于加载圆形头像我们并不陌生,我只是想记录一下glide加载圆形头像加边框用法
      Glide.with(imageView.getContext())                .load(imageUrl)                .transform(new GlideCircleTransform(imageView.getContext()))                .into(imageView);
transform
public class GlideCircleTransform extends BitmapTransformation {    public GlideCircleTransform(Context context) {        super(context);    }    @Override    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {        return circleCrop(pool, toTransform);    }    private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {        if (source == null) return null;        int size = Math.min(source.getWidth(), source.getHeight());        int x = (source.getWidth() - size) / 2;        int y = (source.getHeight() - size) / 2;        // TODO this could be acquired from the pool too        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);        //绘制边框        Paint mBorderPaint = new Paint();        mBorderPaint.setStyle(Paint.Style.STROKE);        mBorderPaint.setStrokeWidth(4);//画笔宽度为4px        mBorderPaint.setColor(Color.WHITE);//边框颜色        mBorderPaint.setStrokeCap(Paint.Cap.ROUND);        mBorderPaint.setAntiAlias(true);        float r = size / 2f;        float r1=(size-2*4)/2f;        canvas.drawCircle(r, r, r1, paint);        canvas.drawCircle(r,r,r1,mBorderPaint);//画边框        return result;    }    @Override    public String getId() {        return getClass().getName();    }}