Skip to content

Instantly share code, notes, and snippets.

@rajeefmk
Last active April 28, 2017 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajeefmk/ffd661b229b5eba070e1caedd3aadc50 to your computer and use it in GitHub Desktop.
Save rajeefmk/ffd661b229b5eba070e1caedd3aadc50 to your computer and use it in GitHub Desktop.
Utility class for various image related manipulations.
/*
This method will create a spannable which is a displayable styled text. It
also has a custom ImageGetter based on Picasso for loading <img> tags inside the html.
We use this for rendering formulas in challenges
*/
public static Spannable getSpannableHtmlWithImageGetter(AppTextView view, String value) {
PicassoImageGetter imageGetter = new PicassoImageGetter(view);
Spannable html;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
html = (Spannable) Html.fromHtml(value, Html.FROM_HTML_MODE_LEGACY, imageGetter, null);
} else {
html = (Spannable) Html.fromHtml(value, imageGetter, null);
}
return html;
}
/*
Used for setting click listener on the formulas loaded in textview / html. This is
done using ImageSpan which detects the Image content inside the spannable.
After that it sets a onClick listener using URLSpan. This is done for all the <img> inside
the html.
*/
public static void setClickListenerOnHtmlImageGetter(Spannable html, final Callback callback) {
for (final ImageSpan span : html.getSpans(0, html.length(), ImageSpan.class)) {
int flags = html.getSpanFlags(span);
int start = html.getSpanStart(span);
int end = html.getSpanEnd(span);
html.setSpan(new URLSpan(span.getSource()) {
@Override
public void onClick(View v) {
callback.onImageClick(span.getSource());
}
}, start, end, flags);
}
}
public interface Callback {
void onImageClick(String imageUrl);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment