Android中使用Glide加载圆形图像或给图片设置指定圆角

慈云数据 6个月前 (05-14) 技术支持 73 0

一、Glide加载圆形头像

效果

在这里插入图片描述

R.mIPmap.head_iCon是默认圆形头像

ImageView mImage = findViewById(R.id.image);
Requestoptions options = new RequestOptions()
        .placeholder(R.mipmap.head_icon)
        .circleCropTransform();
Glide.with(this)
        .load("图像Uri")
        .apply(options)
        .into(mImage);

二、Glide给图像设置圆角

例子:设置图片圆角为10dp

效果

在这里插入图片描述

RequestOptions options = new RequestOptions()
        .placeholder(R.drawable.capture_default)
        .bitmapTransform(new RoundedCorners(dip2px(mContext, 10)));
Glide.with(this)
        .load("图像Uri")
        .apply(options)
        .into(mImage);

单位转换方法

public static int dip2px(Context context, float dPValue) {
        final float scale = context.getResources().getDISPlayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

三、完成,Nice!

微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon