Merge pull request #96 from RichardLuo0/4.0-dev

fix:修复贴吧表情图片高度与字高不一致,以及只有贴吧表情的贴子底部被截断问题
This commit is contained in:
HuanCheng65 2022-04-22 23:47:31 +08:00 committed by GitHub
commit 180561ed10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 4 deletions

View File

@ -8,10 +8,11 @@ import android.text.style.ImageSpan;
import androidx.annotation.DrawableRes; import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class EmotionSpan extends ImageSpan { public class EmotionSpan extends ImageSpan {
public static final String TAG = EmotionSpan.class.getSimpleName(); public static final String TAG = EmotionSpan.class.getSimpleName();
private int size; private final int size;
public EmotionSpan(Context context, @DrawableRes int resId, int size) { public EmotionSpan(Context context, @DrawableRes int resId, int size) {
super(context, resId, ALIGN_BASELINE); super(context, resId, ALIGN_BASELINE);
@ -25,13 +26,27 @@ public class EmotionSpan extends ImageSpan {
return drawable; return drawable;
} }
@Override
public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, @Nullable Paint.FontMetricsInt fm) {
if (fm != null) {
Paint.FontMetricsInt fontFm = paint.getFontMetricsInt();
fm.ascent = fontFm.top;
fm.descent = fontFm.bottom;
fm.top = fm.ascent;
fm.bottom = fm.descent;
}
return size;
}
@Override @Override
public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end,
float x, int top, int y, int bottom, @NonNull Paint paint) { float x, int top, int y, int bottom, @NonNull Paint paint) {
Paint.FontMetricsInt fm = paint.getFontMetricsInt(); Paint.FontMetricsInt fm = paint.getFontMetricsInt();
Drawable drawable = getDrawable(); Drawable drawable = getDrawable();
int transY = (y + fm.descent + y + fm.ascent) / 2 int transY = y - drawable.getBounds().bottom + fm.descent;
- drawable.getBounds().bottom / 2;
canvas.save(); canvas.save();
canvas.translate(x, transY); canvas.translate(x, transY);
drawable.draw(canvas); drawable.draw(canvas);

View File

@ -3,6 +3,7 @@ package com.huanchengfly.tieba.post.utils;
import android.content.Context; import android.content.Context;
import android.text.Spannable; import android.text.Spannable;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.TextPaint;
import android.text.TextUtils; import android.text.TextUtils;
import android.widget.TextView; import android.widget.TextView;
@ -32,7 +33,8 @@ public class StringUtil {
int start = matcherEmotion.start(); int start = matcherEmotion.start();
int imgRes = EmotionUtil.getImgByName(emotion_map_type, key); int imgRes = EmotionUtil.getImgByName(emotion_map_type, key);
if (imgRes != -1) { if (imgRes != -1) {
int size = (int) tv.getTextSize(); TextPaint paint = tv.getPaint();
int size = Math.round(-paint.ascent() + paint.descent());
/* /*
Bitmap bitmap = BitmapFactory.decodeResource(res, imgRes); Bitmap bitmap = BitmapFactory.decodeResource(res, imgRes);
Bitmap scaleBitmap = Bitmap.createScaledBitmap(bitmap, size, size, true); Bitmap scaleBitmap = Bitmap.createScaledBitmap(bitmap, size, size, true);