Skip to content

Instantly share code, notes, and snippets.

@koral--
Created Nov 24, 2015
Embed
What would you like to do?
Drawable.Callback gifCallback;
private SpannableStringBuilder handleEmotion(final TextView gifTextView, String content) {
SpannableStringBuilder sb = new SpannableStringBuilder(content);
String regex = "\\[(.+?)\\]";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(content);
if (m.groupCount() > 0) {
gifCallback = new Drawable.Callback() {
@Override
public void invalidateDrawable(Drawable drawable) {
gifTextView.invalidate();
}
@Override
public void scheduleDrawable(Drawable who, Runnable what, long when) {
gifTextView.postDelayed(what, when);
}
@Override
public void unscheduleDrawable(Drawable who, Runnable what) {
gifTextView.removeCallbacks(what);
}
};
}
while (m.find()) {
String tempText = m.group(1);
String emotionFile = EmotionUtils.getFileByText(tempText);
try {
if (emotionFile != null) {
String gif = "emotion/" + emotionFile + ".gif";
GifDrawable gifDrawable = new GifDrawable(mContext.getAssets(), gif);
gifDrawable.setLoopCount(0);
gifDrawable.setBounds(0, 0, gifDrawable.getIntrinsicWidth(), gifDrawable.getIntrinsicHeight());
sb.setSpan(new ImageSpan(gifDrawable), m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
gifDrawable.setCallback(gifCallback);
}
} catch (Exception e) {
}
}
return sb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment