我的 IM 应用程序必须支持表情符号。它们是 GIF 并具有文本表示,如果用户选择其中之一,则会在输入框中使用。但我想在它们发送后将它们显示为图像。目前我的自定义数组适配器在一行的 TextView 中显示发送的消息。
根据文本表示的出现动态显示图像的正确方法是什么?我是否必须搜索表情符号文本,如果找到,请从布局中删除 TextView(relativeLayout 最适合?)并添加一个带有 IM 开头的 TextView、一个带有表情符号的 ImageView 和另一个 TextView。如果同时发送更多表情符号可能会很困惑。
有没有更简单更合乎逻辑的方法?
最佳答案
我认为构建 Spannable
会更有用。
private static final Factory spannableFactory = Spannable.Factory
.getInstance();
private static final Map<Pattern, Integer> emoticons = new HashMap<Pattern, Integer>();
static {
addPattern(emoticons, ":)", R.drawable.emo_im_happy);
addPattern(emoticons, ":-)", R.drawable.emo_im_happy);
// ...
}
private static void addPattern(Map<Pattern, Integer> map, String smile,
int resource) {
map.put(Pattern.compile(Pattern.quote(smile)), resource);
}
public static boolean addSmiles(Context context, Spannable spannable) {
boolean hasChanges = false;
for (Entry<Pattern, Integer> entry : emoticons.entrySet()) {
Matcher matcher = entry.getKey().matcher(spannable);
while (matcher.find()) {
boolean set = true;
for (ImageSpan span : spannable.getSpans(matcher.start(),
matcher.end(), ImageSpan.class))
if (spannable.getSpanStart(span) >= matcher.start()
&& spannable.getSpanEnd(span) <= matcher.end())
spannable.removeSpan(span);
else {
set = false;
break;
}
if (set) {
hasChanges = true;
spannable.setSpan(new ImageSpan(context, entry.getValue()),
matcher.start(), matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
return hasChanges;
}
public static Spannable getSmiledText(Context context, CharSequence text) {
Spannable spannable = spannableFactory.newSpannable(text);
addSmiles(context, spannable);
return spannable;
}
实际上这段代码基于原生 Html
类的源代码。
编辑:更新版本显着提高了速度。
关于android - 在 Android 中显示表情符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3341702/