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?
There is a transformation library for Picasso.
Just add gradle dependency
implementation 'jp.wasabeef:picasso-transformations:2.2.1'
End use it
Picasso.with(context)
.load(url)
.resize(w, h)
.transform(new CropCircleTransformation())
.into(imageview);
Wiki: Picasso Transformations
Use this library to create a circular imageview. To make a circular ImageView, add this CircularImageView library to your project and add CircularImageView in your layout XML
<com.pkmmte.view.CircularImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:src="@drawable/image"
app:border_color="#EEEEEE"
app:border_width="4dp"
app:shadow="true" />`
Then use picasso to load required image into this imageView. Picasso does all the caching you dont need to worry about it
Another alternative I found was this guys library. It works standalone, or in conjunction with Picasso. I chose the Picasso route, like below:
https://github.com/vinc3m1/RoundedImageView
Transformation transformation = new RoundedTransformationBuilder()
.borderColor(Color.BLACK)
.borderWidthDp(3)
.cornerRadiusDp(30)
.oval(false)
.build();
Picasso.with(context)
.load(url)
.fit()
.transform(transformation)
.into(imageView);
Worked for me!
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);
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!
Here is what worked for me with Picasso v2.71828
class CircleTransform : Transformation {
override fun transform(source: Bitmap?): Bitmap? {
if (source == null) {
return source
}
var bitmap: Bitmap
// since we cant transform hardware bitmaps create a software copy first
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && source.config == Bitmap.Config.HARDWARE) {
val softwareCopy = source.copy(Bitmap.Config.ARGB_8888, true)
if (softwareCopy == null) {
return source
} else {
bitmap = softwareCopy
source.recycle()
}
} else {
bitmap = source
}
var size = bitmap.width
// if bitmap is non-square first create square one
if (size != bitmap.height) {
var sizeX = size
var sizeY = bitmap.height
size = Math.min(sizeY, sizeX)
sizeX = (sizeX - size) / 2
sizeY = (sizeY - size) / 2
val squareSource = Bitmap.createBitmap(bitmap, sizeX, sizeY, size, size)
bitmap.recycle()
bitmap = squareSource
}
val circleBitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888)
val canvas = Canvas(circleBitmap)
val paint = Paint()
val shader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
paint.shader = shader
paint.isAntiAlias = true
val centerAndRadius = size / 2f
canvas.drawCircle(centerAndRadius, centerAndRadius, centerAndRadius, paint)
bitmap.recycle()
return circleBitmap
}
override fun key(): String {
return "circleTransformation()"
}
}