android gif表情

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 = context.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(context, BitmapFactory.decodeStream(context.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;

}


你可能感兴趣的:(android gif表情)