Android开发第三方库glide和圆形控件circleimageview一起使用第一次进入无法显示问题

本文介绍如何使用Glide结合RoundedBitmapDrawableFactory解决加载圆形头像时的问题,并实现白色边框及中心裁剪效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用glide加载圆形头像,圆形头像使用控件circleimageview,发现第一进入头像根本不会显示,后来使用android v4包提供的RoundedBitmapDrawableFactory完美的解决了这个问题,在使用RoundedBitmapDrawableFactory时,xml中的控件直接可以使用控件imageview.

在使用RoundedBitmapDrawableFactory时,设置RoundedBitmapDrawableFactory使其带有白色边框,以及从中间裁剪,避免原形图片圆形图片放大的问题,若是不需要带白色边框直接可以传入false,需要显示白色边框传入true.实现如下:

public class ImageUtils {
    public static void loadHeadImage(final Context context, String url, final ImageView photo, final boolean isBord) {
        Glide.with(context).load(url).asBitmap().centerCrop().into(new BitmapImageViewTarget(photo) {
            @Override
            protected void setResource(Bitmap resource) {
                if (isBord==true){
                    Drawable image = setRoundImageWithBorder(context, resource);
                    photo.setImageDrawable(image);
                }else {
                    RoundedBitmapDrawable drawable = setNoBordRoundImageWithBorder(context,resource);
                    photo.setImageDrawable(drawable);
                }
            }
        });
    }

    /**
     * 设置RoundedBitmapDrawableFactory
     * 其方法:
     * setCircular  是否是圆形
     * setAlpha 透明度
     * setAntiAlias抗锯齿
     * setDither 防抖动
     *
     * @param context
     * @param bitmap
     * @return
     */
    private static Drawable setRoundImageWithBorder(Context context, Bitmap bitmap) {
        //获取原图宽高
        int bitmapWidth = bitmap.getWidth();
        int bitmapHeight = bitmap.getHeight();
        //边框宽度 pixel设置20像素
        int borderWidthHalf = 20;
        int bitmapSquareWidth = Math.min(bitmapWidth, bitmapHeight);
        int newBitmapSquareWidth = bitmapSquareWidth + borderWidthHalf;
        Bitmap roundedBitmap = Bitmap.createBitmap(newBitmapSquareWidth, newBitmapSquareWidth, Bitmap.Config.ARGB_8888);
        //裁剪后图像, X,Y除以2进行一个中心裁剪
        Canvas canvas = new Canvas(roundedBitmap);
        int x = borderWidthHalf + bitmapSquareWidth - bitmapWidth;
        int y = borderWidthHalf + bitmapSquareWidth - bitmapHeight;
        canvas.drawBitmap(bitmap, x / 2, y / 2, null);
        //添加图片边框
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(borderWidthHalf);
        paint.setColor(Color.WHITE);  //设置边框为白色
        canvas.drawCircle(canvas.getWidth() / 2, canvas.getWidth() / 2, newBitmapSquareWidth / 2, paint);

        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), roundedBitmap);
        roundedBitmapDrawable.setGravity(Gravity.CENTER);
        roundedBitmapDrawable.setCircular(true);    //设置为原形图片
        return roundedBitmapDrawable;
    }

    /**
     * 不包含白色边框
     * @param context
     * @return
     */
    private static RoundedBitmapDrawable setNoBordRoundImageWithBorder(Context context,Bitmap bitmap){
        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);
        roundedBitmapDrawable.setCircular(true);    //设置为原形图片
        return roundedBitmapDrawable;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值