Skip to content

Instantly share code, notes, and snippets.

@donnfelker
Last active December 2, 2020 20:47
  • Star 90 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save donnfelker/46c56cab8441ecf2ee14 to your computer and use it in GitHub Desktop.
Picasso and CircularTransform Example
import android.graphics.*;
import com.squareup.picasso.Transformation;
/**
* Transforms an image into a circle representation. Such as a avatar.
*/
public class CircularTransformation implements Transformation
{
int radius = 10;
public CircularTransformation(final int radius)
{
this.radius = radius;
}
public CircularTransformation()
{
}
@Override
public Bitmap transform(final Bitmap source)
{
Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, source.getWidth(), source.getHeight());
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
canvas.drawCircle(source.getWidth() / 2 + 0.7f, source.getHeight() / 2 + 0.7f, source.getWidth() / 2 - 1.1f, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(source, rect, rect, paint);
if(source != output) {
source.recycle();
}
return output;
}
@Override
public String key()
{
return "circular" + String.valueOf(radius);
}
}
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
// all other init code
ImageView avatar = findViewById(R.id.avatar);
Picasso.with(this)
.load("http://i.imgur.com/DvpvklR.png")
.transform(new CircularTransformation(10)) // 10 is the radius
.into(avatar);
// Do the happy dance
}
}
@premnirmal
Copy link

Nice work! can we see an example?

@tasomaniac
Copy link

Nice work. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment