How to round an image with Glide library?

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

    You have to use CircularImageView to Display that type of Image...

    You are using Glide library which used to load images..

    Create One ClassFile in your Project and Load it in Imageview... and You will get Desired Result...

    Try Following Code...

    XML

     
    

    CircularImageView.java

    public class CircularImageView extends ImageView {
        private int borderWidth;
        private int canvasSize;
        private Bitmap image;
        private Paint paint;
        private Paint paintBorder;
    
        public CircularImageView(final Context context) {
            this(context, null);
        }
    
        public CircularImageView(Context context, AttributeSet attrs) {
            this(context, attrs, R.attr.circularImageViewStyle);
        }
    
        public CircularImageView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
    
            // init paint
            paint = new Paint();
            paint.setAntiAlias(true);
    
            paintBorder = new Paint();
            paintBorder.setAntiAlias(true);
    
            // load the styled attributes and set their properties
            TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CircularImageView, defStyle, 0);
    
            if(attributes.getBoolean(R.styleable.CircularImageView_border, true)) {
                int defaultBorderSize = (int) (4 * getContext().getResources().getDisplayMetrics().density + 0.5f);
                setBorderWidth(attributes.getDimensionPixelOffset(R.styleable.CircularImageView_border_width, defaultBorderSize));
                setBorderColor(attributes.getColor(R.styleable.CircularImageView_border_color, Color.WHITE));
            }
    
            if(attributes.getBoolean(R.styleable.CircularImageView_shadow, false))
                addShadow();
        }
    
        public void setBorderWidth(int borderWidth) {
            this.borderWidth = borderWidth;
            this.requestLayout();
            this.invalidate();
        }
    
        public void setBorderColor(int borderColor) {
            if (paintBorder != null)
                paintBorder.setColor(borderColor);
            this.invalidate();
        }
    
        public void addShadow() {
            setLayerType(LAYER_TYPE_SOFTWARE, paintBorder);
            paintBorder.setShadowLayer(4.0f, 0.0f, 2.0f, Color.BLACK);
        }
    
        @Override
        public void onDraw(Canvas canvas) {
            // load the bitmap
            image = drawableToBitmap(getDrawable());
    
            // init shader
            if (image != null) {
    
                canvasSize = canvas.getWidth();
                if(canvas.getHeight()

    Note :

    You can use

    CircularImageView imgIcon = (CircularImageView)findViewById(R.id.imageview);
    

    or

    ImageView imgIcon = (ImageView)findViewById(R.id.imageview);
    

    it wont affect your other libraries... dont have to change your code for downloading image or anything else... it simply can be defined using XML too..

提交回复
热议问题