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); composeAvatar.setImageResource(R.drawable.avatar_default);
} else { } else {
Picasso.with(this).load(activeAccount.getProfilePictureUrl()) Picasso.with(this).load(activeAccount.getProfilePictureUrl())
.transform(new RoundedTransformation(7, 0)) .transform(new RoundedTransformation(25))
.error(R.drawable.avatar_default) .error(R.drawable.avatar_default)
.placeholder(R.drawable.avatar_default) .placeholder(R.drawable.avatar_default)
.into(composeAvatar); .into(composeAvatar);

View file

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

View file

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

View file

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

View file

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

View file

@ -145,7 +145,7 @@ public class NotificationHelper {
try { try {
accountAvatar = Picasso.with(context) accountAvatar = Picasso.with(context)
.load(body.getAccount().getAvatar()) .load(body.getAccount().getAvatar())
.transform(new RoundedTransformation(7, 0)) .transform(new RoundedTransformation(20))
.get(); .get();
} catch (IOException e) { } catch (IOException e) {
Log.d(TAG, "error loading account avatar", 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 { public class RoundedTransformation implements Transformation {
private final int radius; private final float percent;
private final int margin;
public RoundedTransformation(final int radius, final int margin) { /** 100% would mean a perfectly round image **/
this.radius = radius; public RoundedTransformation(final float percent) {
this.margin = margin; this.percent = percent;
} }
@Override @Override
public Bitmap transform(Bitmap source) { 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.setAntiAlias(true);
paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); 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 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) { if (source != output) {
source.recycle(); source.recycle();
@ -55,6 +65,6 @@ public class RoundedTransformation implements Transformation {
@Override @Override
public String key() { public String key() {
return "rounded"; return "rounded "+percent+"%";
} }
} }