Use Glide to load circular images or specify rounded corners for images in Android

1. Glide loads circular avatar

Effect
Insert image description here
R.mipmap.head_icon is the default round avatar

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);

2. Glide sets rounded corners to the image

Example: Set the image rounded corners to 10dp
Effect
Insert image description here

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

Unit conversion method

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

3. Done, Nice!

Guess you like

Origin blog.csdn.net/qq_46269365/article/details/133924868