From 60cef27c86fce3fd1d0d685cdeb009891d5608e9 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Mon, 13 Mar 2017 16:19:34 +0100 Subject: [PATCH] Rounded corners on avatars in statuses --- .../tusky/MyFirebaseMessagingService.java | 1 + .../tusky/RoundedTransformation.java | 45 +++++++++++++++++++ .../keylesspalace/tusky/StatusViewHolder.java | 1 + app/src/main/res/layout/item_status.xml | 2 +- 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/keylesspalace/tusky/RoundedTransformation.java diff --git a/app/src/main/java/com/keylesspalace/tusky/MyFirebaseMessagingService.java b/app/src/main/java/com/keylesspalace/tusky/MyFirebaseMessagingService.java index 31060869e..785f02e0c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MyFirebaseMessagingService.java +++ b/app/src/main/java/com/keylesspalace/tusky/MyFirebaseMessagingService.java @@ -160,6 +160,7 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService { Picasso.with(this) .load(body.account.avatar) .placeholder(R.drawable.avatar_default) + .transform(new RoundedTransformation(7, 0)) .into(mTarget); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { diff --git a/app/src/main/java/com/keylesspalace/tusky/RoundedTransformation.java b/app/src/main/java/com/keylesspalace/tusky/RoundedTransformation.java new file mode 100644 index 000000000..0ac1a4ac2 --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/RoundedTransformation.java @@ -0,0 +1,45 @@ +package com.keylesspalace.tusky; + +import android.graphics.Bitmap; +import android.graphics.BitmapShader; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.RectF; +import android.graphics.Shader; + +import com.squareup.picasso.Transformation; + +public class RoundedTransformation implements Transformation { + + private final int radius; + private final int margin; + + public RoundedTransformation(final int radius, final int margin) { + this.radius = radius; + this.margin = margin; + } + + @Override + public Bitmap transform(Bitmap source) { + 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); + Canvas canvas = new Canvas(output); + + canvas.drawRoundRect(new RectF(margin, margin, source.getWidth() - margin, source.getHeight() - margin), radius, radius, paint); + + if (source != output) { + source.recycle(); + } + + return output; + } + + @Override + public String key() { + return "rounded"; + } +} diff --git a/app/src/main/java/com/keylesspalace/tusky/StatusViewHolder.java b/app/src/main/java/com/keylesspalace/tusky/StatusViewHolder.java index 35168d048..619b187c3 100644 --- a/app/src/main/java/com/keylesspalace/tusky/StatusViewHolder.java +++ b/app/src/main/java/com/keylesspalace/tusky/StatusViewHolder.java @@ -158,6 +158,7 @@ class StatusViewHolder extends RecyclerView.ViewHolder { .load(url) .placeholder(R.drawable.avatar_default) .error(R.drawable.avatar_error) + .transform(new RoundedTransformation(7, 0)) .into(avatar); } diff --git a/app/src/main/res/layout/item_status.xml b/app/src/main/res/layout/item_status.xml index b6bde1132..5525477f7 100644 --- a/app/src/main/res/layout/item_status.xml +++ b/app/src/main/res/layout/item_status.xml @@ -40,7 +40,7 @@ android:scaleType="fitCenter" android:id="@+id/status_avatar" android:layout_below="@+id/status_reblogged_bar" - android:layout_marginTop="10dp" + android:layout_marginTop="11dp" android:layout_marginRight="10dp" />