improve custom emoji size/padding/alignment, fix jumping of text when emojis are loaded

This commit is contained in:
Conny Duck 2017-10-24 23:10:15 +02:00
parent 4736462911
commit b213941c5b

View file

@ -5,7 +5,6 @@ import android.content.SharedPreferences;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.preference.PreferenceManager; 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 private @Nullable
Drawable imageDrawable; Drawable imageDrawable;
private WeakReference<Callback> callbackWeakReference; private WeakReference<Callback> callbackWeakReference;
private Context context; private Context context;
public EmojiSpan(Context context) { EmojiSpan(Context context) {
this.context = context.getApplicationContext(); this.context = context.getApplicationContext();
} }
public void setImageDrawable(@Nullable Drawable imageDrawable) {
this.imageDrawable = imageDrawable;
}
public void setCallback(Callback callback) { public void setCallback(Callback callback) {
this.callbackWeakReference = new WeakReference<>(callback); this.callbackWeakReference = new WeakReference<>(callback);
} }
@ -536,10 +531,7 @@ class StatusBaseViewHolder extends RecyclerView.ViewHolder {
@Override @Override
public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, public int getSize(@NonNull Paint paint, CharSequence text, int start, int end,
@Nullable Paint.FontMetricsInt fm) { @Nullable Paint.FontMetricsInt fm) {
if (imageDrawable == null) return 0; return (int) (paint.getTextSize()*1.2);
int textSize = (int) paint.getTextSize();
imageDrawable.setBounds(0, 0, textSize, textSize);
return textSize;
} }
@Override @Override
@ -547,8 +539,12 @@ class StatusBaseViewHolder extends RecyclerView.ViewHolder {
int top, int y, int bottom, @NonNull Paint paint) { int top, int y, int bottom, @NonNull Paint paint) {
if (imageDrawable == null) return; if (imageDrawable == null) return;
canvas.save(); canvas.save();
int emojiSize = (int) (paint.getTextSize() * 1.1);
imageDrawable.setBounds(0, 0, emojiSize, emojiSize);
int transY = bottom - imageDrawable.getBounds().bottom; int transY = bottom - imageDrawable.getBounds().bottom;
transY -= paint.getFontMetricsInt().descent; transY -= paint.getFontMetricsInt().descent/2;
canvas.translate(x, transY); canvas.translate(x, transY);
imageDrawable.draw(canvas); imageDrawable.draw(canvas);
canvas.restore(); canvas.restore();