Glide rounded corners image with custom Glide target

suggest change

Put this method in a utility class, or in the class that loads the image. asBitmap() and BitmapImageViewTarget are Glide 3 APIs.

public class UIUtils {
    public static BitmapImageViewTarget getRoundedImageTarget(@NonNull final Context context, @NonNull final ImageView imageView,
                                                            final float radius) {
        return new BitmapImageViewTarget(imageView) {
            @Override
            protected void setResource(final Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                circularBitmapDrawable.setCornerRadius(radius);
                imageView.setImageDrawable(circularBitmapDrawable);
            }
        };
    }
}

Loading image:

Glide.with(context)
     .load(imageUrl)
     .asBitmap()
     .into(UIUtils.getRoundedImageTarget(context, imageView, radius));

Because you use asBitmap() the animations will be removed though. You can use your own animation in this place using the animate() method.

Example with similar fade in to default Glide animation.

Glide.with(context)
     .load(imageUrl)
     .asBitmap()
     .animate(R.anim.abc_fade_in)
     .into(UIUtils.getRoundedImageTarget(context, imageView, radius));

Please note this animation is a support-library private resource. Using it is discouraged because it can change or be removed.

Note you also need to have support library to use RoundedBitmapDrawableFactory

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents