Make ImageView with Round Corner Using picasso

后端 未结 7 977
刺人心
刺人心 2020-12-15 03:14

I know there are lots of link available to make ImageView Round Corner. But I\'m Using Picasso Library for Image Loading.. I refer the link to get

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 03:39

    Following @stevyhacker's answer and this related one, I came up with this:

    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.graphics.PorterDuff;
    import android.graphics.PorterDuffXfermode;
    import android.graphics.Rect;
    import android.graphics.RectF;
    
    import com.squareup.picasso.Transformation;
    
    
    public class RoundedCornersTransform implements Transformation {
        private static Bitmap createRoundedRectBitmap(Bitmap bitmap,
                                                      float topLeftCorner, float topRightCorner,
                                                      float bottomRightCorner, float bottomLeftCorner) {
            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                    Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(output);
    
            final int color = Color.WHITE;
            final Paint paint = new Paint();
            final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            final RectF rectF = new RectF(rect);
            Path path = new Path();
            float[] radii = new float[]{
                    topLeftCorner, bottomLeftCorner,
                    topRightCorner, topRightCorner,
                    bottomRightCorner, bottomRightCorner,
                    bottomLeftCorner, bottomLeftCorner
            };
    
            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            path.addRoundRect(rectF, radii, Path.Direction.CW);
            canvas.drawPath(path, paint);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);
            return output;
        }
    
        @Override
        public Bitmap transform(Bitmap source) {
            int size = Math.min(source.getWidth(), source.getHeight());
    
            int x = (source.getWidth() - size) / 2;
            int y = (source.getHeight() - size) / 2;
    
            Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
            if (squaredBitmap != source) {
                source.recycle();
            }
    
            float r = size / 4f;
    
            Bitmap roundedBitmap = createRoundedRectBitmap(squaredBitmap, r, r, r, r);
    
            squaredBitmap.recycle();
    
            return roundedBitmap;
        }
    
        @Override
        public String key() {
            return "rounded_corners";
        }
    }
    

    Use it like this:

    Picasso.with(context).load(url).transform(new RoundedCornersTransform()).into(imageView);
    

    Probably needs some enhancements though, so watch out!

提交回复
热议问题