From b213941c5b7e58a43b2dc6bd6f54a291b7c9b281 Mon Sep 17 00:00:00 2001 From: Conny Duck Date: Tue, 24 Oct 2017 23:10:15 +0200 Subject: [PATCH] improve custom emoji size/padding/alignment, fix jumping of text when emojis are loaded --- .../tusky/adapter/StatusBaseViewHolder.java | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) 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 5160bb5b..0ce84332 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java @@ -5,7 +5,6 @@ import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; -import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.preference.PreferenceManager; @@ -514,21 +513,17 @@ class StatusBaseViewHolder extends RecyclerView.ViewHolder { } } - static class EmojiSpan extends ReplacementSpan implements Target { + private static class EmojiSpan extends ReplacementSpan implements Target { private @Nullable Drawable imageDrawable; private WeakReference callbackWeakReference; private Context context; - public EmojiSpan(Context context) { + EmojiSpan(Context context) { this.context = context.getApplicationContext(); } - public void setImageDrawable(@Nullable Drawable imageDrawable) { - this.imageDrawable = imageDrawable; - } - public void setCallback(Callback callback) { this.callbackWeakReference = new WeakReference<>(callback); } @@ -536,10 +531,7 @@ class StatusBaseViewHolder extends RecyclerView.ViewHolder { @Override public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, @Nullable Paint.FontMetricsInt fm) { - if (imageDrawable == null) return 0; - int textSize = (int) paint.getTextSize(); - imageDrawable.setBounds(0, 0, textSize, textSize); - return textSize; + return (int) (paint.getTextSize()*1.2); } @Override @@ -547,8 +539,12 @@ class StatusBaseViewHolder extends RecyclerView.ViewHolder { int top, int y, int bottom, @NonNull Paint paint) { if (imageDrawable == null) return; canvas.save(); + + int emojiSize = (int) (paint.getTextSize() * 1.1); + imageDrawable.setBounds(0, 0, emojiSize, emojiSize); + int transY = bottom - imageDrawable.getBounds().bottom; - transY -= paint.getFontMetricsInt().descent; + transY -= paint.getFontMetricsInt().descent/2; canvas.translate(x, transY); imageDrawable.draw(canvas); canvas.restore();