androidbitmapimageviewpicasso

android: create circular image with picasso


The question had been asked and there had been a promise made for the very version of Picasso that I am using: How do I send a circular bitmap to an ImageView using Picasso? I am new to Picasso and only thing I have used is

Picasso.with(context).load(url).resize(w, h).into(imageview);

I have already found https://gist.github.com/julianshen/5829333 but I am not sure how to combine it with the line above in a non-awkward way.


Solution

  • Research a bit before as there are answers available. Anyhow, follow This Link and read it carefully to know how to use it.

    try this:

    import com.squareup.picasso.Transformation;
    
    public class CircleTransform implements Transformation {
        @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();
            }
    
            Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
    
            Canvas canvas = new Canvas(bitmap);
            Paint paint = new Paint();
            BitmapShader shader = new BitmapShader(squaredBitmap,
                    Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            paint.setShader(shader);
            paint.setAntiAlias(true);
    
            float r = size / 2f;
            canvas.drawCircle(r, r, r, paint);
    
            squaredBitmap.recycle();
            return bitmap;
        }
    
        @Override
        public String key() {
            return "circle";
        }
    }
    

    then simply apply it like:

    Picasso.with(activity).load(mayorShipImageLink).transform(new CircleTransform()).into(ImageView);