Created
November 24, 2015 17:09
-
-
Save koral--/815ff6303e04b074386d to your computer and use it in GitHub Desktop.
Possible fix for https://github.com/koral--/android-gif-drawable/issues/228
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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