标签:
/**
* 解析表情
* @param gifTextView
* @param content
* @return
*/
private SpannableStringBuilder handler(final TextView gifTextView,String content) {
SpannableStringBuilder sb = new SpannableStringBuilder(content);
String regex = "(\\#\\[face/png/f_static_)\\d{3}(.png\\]\\#)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(content);
while (m.find()) {
String tempText = m.group();
try {
String num = tempText.substring("#[face/png/f_static_".length(), tempText.length()- ".png]#".length());
String gif = "face/gif/f" + num + ".gif";
/**
* 如果open这里不抛异常说明存在gif,则显示对应的gif
* 否则说明gif找不到,则显示png
* */
InputStream is = mContext.getAssets().open(gif);
sb.setSpan(new AnimatedImageSpan(new AnimatedGifDrawable(is,new AnimatedGifDrawable.UpdateListener() {
@Override
public void update() {
gifTextView.postInvalidate();
}
})), m.start(), m.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
is.close();
} catch (Exception e) {
String png = tempText.substring("#[".length(),tempText.length() - "]#".length());
try {
sb.setSpan(new ImageSpan(mContext, BitmapFactory.decodeStream(mContext.getAssets().open(png))), m.start(), m.end(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
}
return sb;
}
标签:
原文地址:http://www.cnblogs.com/renzhe2016/p/5226746.html