So, anybody know how to display an image with rounded corners with Glide? I am loading an image with Glide, but I don\'t know how to pass rounded params to this library.
Here is a more modular and cleaner way to circle crop your bitmap in Glide:
BitmapTransformation
then override transform
method like this : For Glide 4.x.x
public class CircularTransformation extends BitmapTransformation {
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(null, toTransform);
circularBitmapDrawable.setCircular(true);
Bitmap bitmap = pool.get(outWidth, outHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
circularBitmapDrawable.setBounds(0, 0, outWidth, outHeight);
circularBitmapDrawable.draw(canvas);
return bitmap;
}
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {}
}
For Glide 3.x.x
public class CircularTransformation extends BitmapTransformation {
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(null, toTransform);
circularBitmapDrawable.setCircular(true);
Bitmap bitmap = pool.get(outWidth, outHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
circularBitmapDrawable.setBounds(0, 0, outWidth, outHeight);
circularBitmapDrawable.draw(canvas);
return bitmap;
}
@Override
public String getId() {
// Return some id that uniquely identifies your transformation.
return "CircularTransformation";
}
}
Glide.with(yourActivity)
.load(yourUrl)
.asBitmap()
.transform(new CircularTransformation())
.into(yourView);
Hope this helps :)
private void setContactImage(@NonNull ViewHolder holder, ClsContactDetails clsContactDetails) {
Glide.with(context).load(clsContactDetails.getPic())
.apply(new RequestOptions().centerCrop().circleCrop().placeholder(R.mipmap.ic_launcher)).into(holder.ivPersonImage);
}
Try this way
Glide.with(this)
.load(R.drawable.thumbnail)
.bitmapTransform(new CropCircleTransformation(this))
.into(mProfile);
<ImageView
android:id="@+id/img_profile"
android:layout_width="76dp"
android:layout_height="76dp"
android:background="@drawable/all_circle_white_bg"
android:padding="1dp"/>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="@android:color/white"/>
</shape>
</item>
</selector>
Use this transformation, it will work fine.
public class CircleTransform extends BitmapTransformation {
public CircleTransform(Context context) {
super(context);
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return circleCrop(pool, toTransform);
}
private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
int borderColor = ColorUtils.setAlphaComponent(Color.WHITE, 0xFF);
int borderRadius = 3;
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
// TODO this could be acquired from the pool too
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
if (squared != source) {
source.recycle();
}
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
// Prepare the background
Paint paintBg = new Paint();
paintBg.setColor(borderColor);
paintBg.setAntiAlias(true);
// Draw the background circle
canvas.drawCircle(r, r, r, paintBg);
// Draw the image smaller than the background so a little border will be seen
canvas.drawCircle(r, r, r - borderRadius, paint);
squared.recycle();
return result;
}
@Override
public String getId() {
return getClass().getName();
}}