How to round an image with Glide library?

前端 未结 22 1852
悲哀的现实
悲哀的现实 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:05

    I was looking for it earlier and I made it in very easiest way, I hope you would like this.

     //crete this method into your Utils class and call this method wherever you want to use.
        //you can set these placeHolder() and error() image static as well. I made it as comment inside this method, then no need to use [placeHolderUrl and errorImageUrl] parameters. remove it from this method.
        public static void loadImage(final Activity context, ImageView imageView, String url, int placeHolderUrl, int errorImageUrl) {
            if (context == null || context.isDestroyed()) return;
    
            //placeHolderUrl=R.drawable.ic_user;
            //errorImageUrl=R.drawable.ic_error;
                Glide.with(context) //passing context
                        .load(getFullUrl(url)) //passing your url to load image.
                        .placeholder(placeHolderUrl) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url.
                        .error(errorImageUrl)//in case of any glide exception or not able to download then this image will be appear . if you won't mention this error() then nothing to worry placeHolder image would be remain as it is.
                        .diskCacheStrategy(DiskCacheStrategy.ALL) //using to load into cache then second time it will load fast.
                        .transform(new CircleTransform(context))//this CircleTransform class help to crop an image as circle.
                        .animate(R.anim.fade_in) // when image (url) will be loaded by glide then this face in animation help to replace url image in the place of placeHolder (default) image.
                        .fitCenter()//this method help to fit image into center of your ImageView
                        .into(imageView); //pass imageView reference to appear the image.
        } 
    

    CircleTransform.java

      public class CircleTransform extends BitmapTransformation {
        public CircleTransform(Context context) {
            super(context);
    
            if(context==null)
                return;
        }
    
        private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
            if (source == null) return null;
    
            int size = Math.min(source.getWidth(), source.getHeight());
            int x = (source.getWidth() - size) / 2;
            int y = (source.getHeight() - size) / 2;
    
    
            Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
    
            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);
            return result;
        }
    
        @Override
        protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            return circleCrop(pool, toTransform);
        }
    
        @Override
        public String getId() {
            return getClass().getName();
        }
    }
    

    fade_in.xml for fade in animation .

        
    
    
    
    

    finally to call this method.

    Utils.loadImage(YourClassName.this,mImageView,url,R.drawable.ic_user,R.drawable.ic_error);
    

提交回复
热议问题