Glide 圆形头像的使用

注意事项:

由于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);
    }
});


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值