在Android中显示图释

我的IM应用必须支持表情符号。它们是GIF并具有文本表示形式,如果用户选择其中之一,则会在输入框中使用它们。但是我想在发送后将它们显示为图像。当前,我的自定义数组适配器在一行的TextView中显示已发送的消息。


有什么合适的方法可以根据文本表示形式动态显示图像?我是否必须搜索表情符号文本,如果找到了,请从布局中删除TextView(relativeLayout最适合吗?),然后在IM的开头添加一个TextView,一个带有表情符号的ImageView和另一个TextView。如果同时发送更多表情符号,可能会造成混乱。


有没有更简单,更合乎逻辑的方法?


料青山看我应如是
浏览 428回答 3
3回答

www说

我会尝试使用正则表达式用<img>标签替换每个表情符号的所有出现。然后,将该HTML转换为SpannedString viaHtml.fromHtml()。这SpannedString可以被用在setText()呼叫TextView。

芜湖不芜

我认为构建会更有用Spannable。private static final Factory spannableFactory = Spannable.Factory&nbsp; &nbsp; &nbsp; &nbsp; .getInstance();private static final Map<Pattern, Integer> emoticons = new HashMap<Pattern, Integer>();static {&nbsp; &nbsp; addPattern(emoticons, ":)", R.drawable.emo_im_happy);&nbsp; &nbsp; addPattern(emoticons, ":-)", R.drawable.emo_im_happy);&nbsp; &nbsp; // ...}private static void addPattern(Map<Pattern, Integer> map, String smile,&nbsp; &nbsp; &nbsp; &nbsp; int resource) {&nbsp; &nbsp; map.put(Pattern.compile(Pattern.quote(smile)), resource);}public static boolean addSmiles(Context context, Spannable spannable) {&nbsp; &nbsp; boolean hasChanges = false;&nbsp; &nbsp; for (Entry<Pattern, Integer> entry : emoticons.entrySet()) {&nbsp; &nbsp; &nbsp; &nbsp; Matcher matcher = entry.getKey().matcher(spannable);&nbsp; &nbsp; &nbsp; &nbsp; while (matcher.find()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean set = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (ImageSpan span : spannable.getSpans(matcher.start(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matcher.end(), ImageSpan.class))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (spannable.getSpanStart(span) >= matcher.start()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && spannable.getSpanEnd(span) <= matcher.end())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spannable.removeSpan(span);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (set) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hasChanges = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spannable.setSpan(new ImageSpan(context, entry.getValue()),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matcher.start(), matcher.end(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return hasChanges;}public static Spannable getSmiledText(Context context, CharSequence text) {&nbsp; &nbsp; Spannable spannable = spannableFactory.newSpannable(text);&nbsp; &nbsp; addSmiles(context, spannable);&nbsp; &nbsp; return spannable;}实际上,此代码基于本机Html类的源代码。

慕标琳琳

如果您使用而不是使用来获取可绘制对象的每个外观的克隆,则似乎不需要整个内部for循环和删除先前的跨度。entry.getValue()entry.getValue().getConstantState().newDrawable()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android