TextView展示富文本时emoj或图片和文字不对齐的解决方案

在项目中,回复框、聊天界面的显示往往会有emoj或者图片,但是一个比较头疼的问题是,会出现emoj表情或者图片和文字的位置不对齐,总是有偏移,这样很影响用户体验的。下面会总结一下如何解决这个问题。

本文所列举的解决方案是参考一个非常给力的表情键盘项目:https://github.com/w446108264/XhsEmoticonsKeyboard

另外一个非常给力的是google提供的开源demo:https://github.com/googlesamples/android-EmojiCompat,文档地址为:https://developer.android.com/guide/topics/ui/look-and-feel/emoji-compat.html#using-emojicompat-for-imes

1. TextView设置

/**
     * 解析评论内容,包含富文本表情
     *
     * @param textView
     * @param text
     */
    private void setRichText(TextView textView, String text) {
        // 将文本转换为span
        Spanned span = Html.fromHtml(text);
        int fontSize = UIHelper.getFontHeight(textView);
        if (TextUtils.isEmpty(text)) {
            textView.setVisibility(View.GONE);
        } else {
            textView.setFocusable(false);
            textView.setLongClickable(false);
            span = InputHelper.displayEmoji(context.getResources(), span, fontSize, Math.round(TDevice.dp2px(5.0f/2.0f)) );
//            去除下划线
            SpannableString spannableString = new SpannableString(span);
            spannableString.setSpan(new NoUnderlineSpan(),0,span.length(),Spanned.SPAN_MARK_MARK);
            textView.setText(spannableString);
        }
    }

2. 展示文字和表情

/**
     * 展示文字和表情
     * @param res
     * @param s
     * @param fontSize 字体大小
     * @param lineSpace 行间距
     * @return
     */
    public static Spannable displayEmoji(Resources res, CharSequence s, int fontSize, int lineSpace) {
        String str = s.toString();
        Spannable spannable;
        if (s instanceof Spannable) {
            spannable = (Spannable) s;
        } else {
            // 构建文字span
            spannable = new SpannableString(str);
        }
        for (int i = 0; i < str.length(); i++) {
            int index1 = str.indexOf("[", i);
            int length1 = str.indexOf("]", index1 + 1);
            int index2 = str.indexOf(":", i);
            try {
                String emojiStr = str.substring(index1, length1 + "]".length());
                int resId = getEmojiResId(emojiStr);
                if (resId > 0) {
                    // 构建图片span
                    Drawable drawable = res.getDrawable(resId);
                    if (drawable != null) {
                        int itemHeight;
                        int itemWidth;
                        if (fontSize == WRAP_DRAWABLE) {
                            itemHeight = drawable.getIntrinsicHeight();
                            itemWidth = drawable.getIntrinsicWidth();
                        } else {
                            itemHeight = fontSize;
                            itemWidth = fontSize;
                        }
                        drawable.setBounds(0, 0, itemHeight, itemWidth);
                        // 自定义ImageSpan,实现文字和表情的对齐
                        EmojiSpan imageSpan = new EmojiSpan(drawable,lineSpace);
                        spannable.setSpan(imageSpan, index1, length1 + "]".length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                    }
                }
            } catch (Exception e) {
            }
        }
        return spannable;
    }

3.处理表情图片的工具类,主要是计算表情的显示大小和位置

public class EmojiSpan extends ImageSpan {
    private int lineSpace;
    public EmojiSpan(Drawable drawable,int lineSpace) {
        super(drawable);
        this.lineSpace = lineSpace;
    }
    public EmojiSpan(Context context, int resourceId) {
        super(context, resourceId);
    }
    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fontMetricsInt) {
        Drawable drawable = this.getDrawable();
        Rect rect = drawable.getBounds();
        if(fontMetricsInt != null) {
            Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
            int fontHeight = fmPaint.bottom - fmPaint.top;
            int drHeight = rect.bottom - rect.top;
            int top = drHeight / 2 - fontHeight / 4;
            int bottom = drHeight / 2 + fontHeight / 4;
            fontMetricsInt.ascent = -bottom;
            fontMetricsInt.top = -bottom;
            fontMetricsInt.bottom = top;
            fontMetricsInt.descent = top;
        }
        return rect.right;
    }
    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
        Drawable drawable = this.getDrawable();
        canvas.save();
        int transY = (bottom - top - drawable.getBounds().bottom) / 2 + top - lineSpace;
        canvas.translate(x, (float)transY);
        drawable.draw(canvas);
        canvas.restore();
    }
}

4.获取TextView字体大小的工具

/**
     * 获取TextView的字体大小
     * @param textView
     * @return
     */
    public static int getFontHeight(TextView textView) {
        Paint paint = new Paint();
        paint.setTextSize(textView.getTextSize());
        Paint.FontMetrics fm = paint.getFontMetrics();
        return (int) Math.ceil(fm.bottom - fm.top);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
安卓中的TextView控件默认是不支持富文本的,即无法直接实现文字可点击的效果。不过我们可以通过一些方法来实现这个需求。 一种常用的方法是使用SpannableString类来实现富文本文字可点击的效果。SpannableString是一个可以调整文字的样式和属性的类。我们可以使用它的setSpan()方法来设置文字的点击事件。 首先,我们需要创建一个ClickableSpan对象,它是一个可以实现文字可点击的类。在ClickableSpan的onClick()方法中,我们可以编写点击文字后的逻辑代码。 ``` ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(View widget) { // 在这里编写点击文字后的逻辑代码 } }; ``` 然后,我们创建一个SpannableString对象,并使用setSpan()方法将ClickableSpan对象应用于需要点击的文字范围。 ``` SpannableString spannableString = new SpannableString("需要设置点击事件的文字"); spannableString.setSpan(clickableSpan, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ``` 其中,startIndex和endIndex分别表示需要设置点击事件的文字的起始和结束位置。 接下来,我们将SpannableString对象设置给TextView控件,并为TextView控件设置setMovementMethod()方法,使其具有点击效果。 ``` textView.setText(spannableString); textView.setMovementMethod(LinkMovementMethod.getInstance()); ``` 最后,我们就可以在TextView中实现文字可点击的效果了。 需要注意的是,在使用这种方法TextView的同三个属性要设置为true:android:focusable="true"、android:focusableInTouchMode="true"和android:clickable="true",以确保TextView本身可以获得焦点和点击事件。 以上就是使用SpannableString实现安卓TextView富文本文字可点击的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值