How to round an image with Glide library?

前端 未结 22 1681
悲哀的现实
悲哀的现实 2020-11-28 00:46

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.

相关标签:
22条回答
  • 2020-11-28 01:17

    Here is a more modular and cleaner way to circle crop your bitmap in Glide:

    1. Create a custom transformation by extending 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";
        }
    
    }
    
    1. Then set it in Glide builder where you need it:
    Glide.with(yourActivity)
       .load(yourUrl)
       .asBitmap()
       .transform(new CircularTransformation())
       .into(yourView);
    

    Hope this helps :)

    0 讨论(0)
  • 2020-11-28 01:18
    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);
    }
    
    0 讨论(0)
  • 2020-11-28 01:19

    Try this way

    code

    Glide.with(this)
        .load(R.drawable.thumbnail)
        .bitmapTransform(new CropCircleTransformation(this))
        .into(mProfile);
    

    XML

    <ImageView
      android:id="@+id/img_profile"
      android:layout_width="76dp"
      android:layout_height="76dp"
      android:background="@drawable/all_circle_white_bg"
      android:padding="1dp"/>
    

    all_circle_white_bg.xml

    <?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>
    
    • I use this transformation library. -> https://github.com/wasabeef/glide-transformations
    • Circle stroke width is ImageView's padding
    0 讨论(0)
  • 2020-11-28 01:19

    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();
    }} 
    
    0 讨论(0)
提交回复
热议问题