From 2851e4d38bf2d644170e12390be0f21e63a1e487 Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Fri, 30 Mar 2018 10:36:19 +0200 Subject: [PATCH] make avatar rounding independent of image size --- .../keylesspalace/tusky/ComposeActivity.java | 2 +- .../adapter/MentionAutoCompleteAdapter.java | 2 +- .../tusky/adapter/NotificationsAdapter.java | 6 ++-- .../tusky/adapter/StatusBaseViewHolder.java | 2 +- .../tusky/adapter/StatusViewHolder.java | 2 +- .../tusky/util/NotificationHelper.java | 2 +- .../tusky/view/RoundedTransformation.java | 28 +++++++++++++------ 7 files changed, 27 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java index 35061ef7..af6e4cad 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java @@ -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); diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/MentionAutoCompleteAdapter.java b/app/src/main/java/com/keylesspalace/tusky/adapter/MentionAutoCompleteAdapter.java index 81e759b6..8b62af1c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/MentionAutoCompleteAdapter.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/MentionAutoCompleteAdapter.java @@ -129,7 +129,7 @@ public class MentionAutoCompleteAdapter extends ArrayAdapter Picasso.with(context) .load(account.getAvatar()) .placeholder(R.drawable.avatar_default) - .transform(new RoundedTransformation(7, 0)) + .transform(new RoundedTransformation(25)) .into(avatar); } } diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/NotificationsAdapter.java b/app/src/main/java/com/keylesspalace/tusky/adapter/NotificationsAdapter.java index 1748fd89..69423012 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/NotificationsAdapter.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/NotificationsAdapter.java @@ -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); } } diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java index 8251b1b6..28531293 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java @@ -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); } } diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusViewHolder.java b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusViewHolder.java index a0263984..9d7f5ce8 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusViewHolder.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusViewHolder.java @@ -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); diff --git a/app/src/main/java/com/keylesspalace/tusky/util/NotificationHelper.java b/app/src/main/java/com/keylesspalace/tusky/util/NotificationHelper.java index fafb502a..5c4530df 100644 --- a/app/src/main/java/com/keylesspalace/tusky/util/NotificationHelper.java +++ b/app/src/main/java/com/keylesspalace/tusky/util/NotificationHelper.java @@ -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); diff --git a/app/src/main/java/com/keylesspalace/tusky/view/RoundedTransformation.java b/app/src/main/java/com/keylesspalace/tusky/view/RoundedTransformation.java index 9a023010..0c992bb7 100644 --- a/app/src/main/java/com/keylesspalace/tusky/view/RoundedTransformation.java +++ b/app/src/main/java/com/keylesspalace/tusky/view/RoundedTransformation.java @@ -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+"%"; } }