make avatar rounding independent of image size

This commit is contained in:
Konrad Pozniak 2018-03-30 10:36:19 +02:00
parent 1af0b6fd48
commit 2851e4d38b
7 changed files with 27 additions and 17 deletions

View file

@ -226,7 +226,7 @@ public final class ComposeActivity extends BaseActivity
composeAvatar.setImageResource(R.drawable.avatar_default);
} else {
Picasso.with(this).load(activeAccount.getProfilePictureUrl())
.transform(new RoundedTransformation(7, 0))
.transform(new RoundedTransformation(25))
.error(R.drawable.avatar_default)
.placeholder(R.drawable.avatar_default)
.into(composeAvatar);

View file

@ -129,7 +129,7 @@ public class MentionAutoCompleteAdapter extends ArrayAdapter<Account>
Picasso.with(context)
.load(account.getAvatar())
.placeholder(R.drawable.avatar_default)
.transform(new RoundedTransformation(7, 0))
.transform(new RoundedTransformation(25))
.into(avatar);
}
}

View file

@ -284,7 +284,7 @@ public class NotificationsAdapter extends RecyclerView.Adapter {
Picasso.with(context)
.load(avatarUrl)
.fit()
.transform(new RoundedTransformation(7, 0))
.transform(new RoundedTransformation(25))
.placeholder(R.drawable.avatar_default)
.into(avatar);
}
@ -445,7 +445,7 @@ public class NotificationsAdapter extends RecyclerView.Adapter {
Picasso.with(context)
.load(statusAvatarUrl)
.placeholder(R.drawable.avatar_default)
.transform(new RoundedTransformation(7, 0))
.transform(new RoundedTransformation(25))
.into(statusAvatar);
}
@ -456,7 +456,7 @@ public class NotificationsAdapter extends RecyclerView.Adapter {
.load(notificationAvatarUrl)
.placeholder(R.drawable.avatar_default)
.fit()
.transform(new RoundedTransformation(7, 0))
.transform(new RoundedTransformation(25))
.into(notificationAvatar);
}
}

View file

@ -120,7 +120,7 @@ abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder {
Picasso.with(avatar.getContext())
.load(url)
.placeholder(R.drawable.avatar_default)
.transform(new RoundedTransformation(7, 0))
.transform(new RoundedTransformation(25))
.into(avatar);
}
}

View file

@ -63,7 +63,7 @@ public class StatusViewHolder extends StatusBaseViewHolder {
Picasso.with(context)
.load(rebloggedUrl)
.fit()
.transform(new RoundedTransformation(7, 0))
.transform(new RoundedTransformation(25))
.into(avatarReblog);
} else {
avatarReblog.setVisibility(View.GONE);

View file

@ -145,7 +145,7 @@ public class NotificationHelper {
try {
accountAvatar = Picasso.with(context)
.load(body.getAccount().getAvatar())
.transform(new RoundedTransformation(7, 0))
.transform(new RoundedTransformation(20))
.get();
} catch (IOException e) {
Log.d(TAG, "error loading account avatar", e);

View file

@ -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+"%";
}
}