Skip to content

Instantly share code, notes, and snippets.

@Sefford
Last active August 29, 2015 14:01
Show Gist options
  • Save Sefford/6e384d0fbd36cd5b2edc to your computer and use it in GitHub Desktop.
Save Sefford/6e384d0fbd36cd5b2edc to your computer and use it in GitHub Desktop.
Picasso Transformation for creating images with rounded corners
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import com.squareup.picasso.Transformation;
/**
* Picasso Transformation for creating images with rounded corners
*
* @author Saul Diaz <[email protected]>
*/
public class RoundedBorderTransformation implements Transformation {
/**
* Pixels to round by corner
*/
private final float pixels;
/**
* Creates a new instance of RoundedBorderTransformation.
*
* @param pixels Pixels to round the corner
*/
public RoundedBorderTransformation(float pixels) {
this.pixels = pixels;
}
@Override
public Bitmap transform(Bitmap source) {
Bitmap output = Bitmap.createBitmap(source.getWidth(), source
.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, source.getWidth(), source.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(source, rect, rect, paint);
source.recycle();
return output;
}
@Override
public String key() {
return "rounded";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment