Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 405
Katedral Pillon Avatar asked Sep 30 '14 03:09

Katedral Pillon


People also ask

How do I make an image Circular Android?

Simply put a circular_crop. png in your drawable folder which is in the shape of your image dimensions (a square in my case) with a white background and a transparent circle in the center. You can use this image if you have want a square imageview.

How do I add an icon to a circular image?

Create a circular drawable. Now add camera icon to imageview using src attribute. Add circular drawable to same imageview using background attribute. Also give padding to it to make camera icon little separated from boundry.


2 Answers

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); 
like image 169
Anirudh Sharma Avatar answered Nov 15 '22 12:11

Anirudh Sharma


here is something that's provided by the support-v4 library! Look into RoundedBitmapDrawable. No need to roll your own:

Picasso.with(context).load(url)                         .resize(w, h)                         .into(myImageView, new Callback() {                             @Override                             public void onSuccess() {                                 Bitmap imageBitmap = ((BitmapDrawable) myImageView.getDrawable()).getBitmap();                                 RoundedBitmapDrawable imageDrawable = RoundedBitmapDrawableFactory.create(getResources(), imageBitmap);                                 imageDrawable.setCircular(true);                                 imageDrawable.setCornerRadius(Math.max(imageBitmap.getWidth(), imageBitmap.getHeight()) / 2.0f);                                 myImageView.setImageDrawable(imageDrawable);                             }                             @Override                             public void onError() {                                 myImageView.setImageResource(R.drawable.default_image);                             }                         }); 

Note: Picasso also has a .transform(customTransformation) call that you could theoretically use, however, I had issues with that. This above works. Good luck!

like image 35
goodKode Avatar answered Nov 15 '22 13:11

goodKode