在项目中,回复框、聊天界面的显示往往会有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(),,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 = ; i < str.length(); i++) {
int index1 = str.indexOf("[", i);
int length1 = str.indexOf("]", index1 + );
int index2 = str.indexOf(":", i);
try {
String emojiStr = str.substring(index1, length1 + "]".length());
int resId = getEmojiResId(emojiStr);
if (resId > ) {
// 构建图片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(, , 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 / - fontHeight / ;
int bottom = drHeight / + fontHeight / ;
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) / + 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);
}

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

  1. wordpress 后台富文本编辑器,添加图片发现无法左对齐,样式出现混乱

    如上图所示,无法左对齐,但是左对齐的按钮全部是正确的,最后一点点排除,发现是因为这个词的影响,去掉就好了,原因不明,可能是这个词被当做某个方法执行了

  2. 【css对齐】块内或者行内图片与文字居中对齐最靠谱的方式!

    块内或者行内图片与文字居中对齐最靠谱的方式! 做图片与文字在一行的按钮时候最常用到,总结了一个靠谱的方法,终于可以完美的对齐下面给个代码 首先是html: <p class="btnU ...

  3. TextView显示HTML文本时<IMG>标签指定图片的显示处理

    TextView显示文本时是支持一些HTML标签的(具体支持那些标签会在下面附录列出),不会需要先用HTML的static方法fromHtml来转换一下. Spanned text = Html.fr ...

  4. Simditor 富文本编辑器多选图片上传、视频连接插入

    simditor 是一个基于浏览器的所见即所得的文本编辑器.Simditor 富文本编辑器, 支持多选图片上传, 视频连接插入, HTML代码编辑以及常用富文本按钮,支持的浏览器:IE10.Firef ...

  5. TextView之富文本

    项目中使用富文本比较常见了,一行显示多种样式颜色的文本,使用 ClickableSpan 富文本实现在同一个 TextView 中的文本的颜色.大小.背景色等属性的多样化和个性化. 我们也可以使用Ht ...

  6. 富文本编辑器TInyMCE,本地图片上传(Image Upload)

    TinyMCE 官网 (类似:百度的富文本web编辑器UEditor) 第一步 下载 TinyMCE,解压后放入工程,在需要的HTML页面引入tinymce.min.js. 第二步 下载tinyMCE ...

  7. angularjs中展示富文本编辑器文本,向DOM中插入元素

    前几天在用textangular富文本编辑器插件时,将存储的文本及格式存储到数据库中,但是从后台接口中再向angular页面插入时却不能执行,即在Angular中操作DOM没有实现,后来查看了一下,操 ...

  8. wangEditor富文本编辑器使用及图片上传

    引入js文件 <script type="text/javascript" src="style/js/wangEditor.min.js">< ...

  9. iView + vue-quill-editor 实现一个富文本编辑器(包含图片,视频上传)

    1. 引入插件(注意IE10以下不支持) npm install vue-quill-editor --savenpm install quill --save (Vue-Quill-Editor需要 ...

随机推荐

  1. 2017-09-16 ADB Shell+Putty

    鼓捣电子词典的时候需要用到ADB Shell.一开始是用cmd.exe,结果发现它不能识别ANSI转义符,就换成了Putty,然后就可以正常使用了,还有彩色. 配置如下: Connection Typ ...

  2. js中的数据类型有

  3. 数据导入Excel时,出现ole error 800AC472这个错误,怎么解决。

    我也出现过这个问题 在生成报表的时候不要动EXCEL中的任何单元格 让它完成保存就可以了 或者是把office 2003 删除下载一个office 2000就可以解决 据说是版本兼容的问题 不是高手 ...

  4. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  5. stark组件开发之编辑功能实现

    编辑功能.和添加一样! 唯一不同的就是, 需要编辑一个指定的  记录.这就需要,在列表页面, 渲染编辑的时候,添加一个 id 值: class UserInfoHandler(StartHandler ...

  6. MySQL开发——【多表关系、引擎、外键、三范式】

    多表关系 一对一关系 一对多或多对一关系 多对多关系 MySQL引擎 所谓的MySQL引擎就是数据的存储方式,常用的数据库引擎有以下几种: Myisam与InnoDB引擎之间的区别(面试) ①批量插入 ...

  7. Node.js web发布到AWS ubuntu 之后,关闭Putty,Node 项目也随之关闭的解决办法

    最近公司把BlockChain和对应的Node Web都发布到了AWS 的ubuntu 系统上. 但是遇到了一个问题,每次启动 Node Web之后,关闭Putty,Node Web也随之关闭. 由于 ...

  8. Luogu3119 草鉴定-Tarjan+Topsort

    Solution 简单的$Tarjan$题. 有大佬现成博客 就不写了 → 传送门 Code #include<cstdio> #include<cstring> #inclu ...

  9. Spring 框架下 事务的配置(复杂)

    //db.properties配置  src下的文件 jdbc.jdbcUrl=jdbc:mysql:///day43jdbc.driverClass=com.mysql.jdbc.Driverjdb ...

  10. String 常用函数

    判断字符串是否包含指定字符str.contains("string"); 查找指定字符索引str.indexOf("s"'); 查找最后出现的字符索引str.i ...