make avatar rounding independent of image size
This commit is contained in:
parent
1af0b6fd48
commit
2851e4d38b
7 changed files with 27 additions and 17 deletions
|
@ -26,25 +26,35 @@ import com.squareup.picasso.Transformation;
|
|||
|
||||
public class RoundedTransformation implements Transformation {
|
||||
|
||||
private final int radius;
|
||||
private final int margin;
|
||||
private final float percent;
|
||||
|
||||
public RoundedTransformation(final int radius, final int margin) {
|
||||
this.radius = radius;
|
||||
this.margin = margin;
|
||||
/** 100% would mean a perfectly round image **/
|
||||
public RoundedTransformation(final float percent) {
|
||||
this.percent = percent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap transform(Bitmap source) {
|
||||
final Paint paint = new Paint();
|
||||
|
||||
final int width = source.getWidth();
|
||||
final int height = source.getHeight();
|
||||
final int shorterSide;
|
||||
if (width > height) {
|
||||
shorterSide = height;
|
||||
} else {
|
||||
shorterSide = width;
|
||||
}
|
||||
|
||||
final float radius = shorterSide / 2 * percent / 100;
|
||||
|
||||
final Paint paint = new Paint();
|
||||
paint.setAntiAlias(true);
|
||||
paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
|
||||
|
||||
Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
|
||||
Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(output);
|
||||
|
||||
canvas.drawRoundRect(new RectF(margin, margin, source.getWidth() - margin, source.getHeight() - margin), radius, radius, paint);
|
||||
canvas.drawRoundRect(new RectF(0, 0, width, height), radius, radius, paint);
|
||||
|
||||
if (source != output) {
|
||||
source.recycle();
|
||||
|
@ -55,6 +65,6 @@ public class RoundedTransformation implements Transformation {
|
|||
|
||||
@Override
|
||||
public String key() {
|
||||
return "rounded";
|
||||
return "rounded "+percent+"%";
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue