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

一、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!

猜你喜欢

转载自blog.csdn.net/qq_46269365/article/details/133924868