Skip to content

Instantly share code, notes, and snippets.

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 jcchikikomori/e8909e93e7f529aeb68848d2c4b5b1e2 to your computer and use it in GitHub Desktop.
Save jcchikikomori/e8909e93e7f529aeb68848d2c4b5b1e2 to your computer and use it in GitHub Desktop.
ImageGetter used for loading remote urls from <img> tag when loaded inside textview.
public class PicassoImageGetter implements Html.ImageGetter {
private AppTextView textView = null;
public PicassoImageGetter() {
}
public PicassoImageGetter(AppTextView target) {
textView = target;
}
@Override
public Drawable getDrawable(String source) {
BitmapDrawablePlaceHolder drawable = new BitmapDrawablePlaceHolder();
Picasso.with(App.get())
.load(source)
.placeholder(R.drawable.img_loading)
.into(drawable);
return drawable;
}
private class BitmapDrawablePlaceHolder extends BitmapDrawable implements Target {
protected Drawable drawable;
@Override
public void draw(final Canvas canvas) {
if (drawable != null) {
drawable.draw(canvas);
}
}
public void setDrawable(Drawable drawable) {
this.drawable = drawable;
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
drawable.setBounds(0, 0, width, height);
setBounds(0, 0, width, height);
if (textView != null) {
textView.setText(textView.getText());
}
}
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
setDrawable(new BitmapDrawable(App.get().getResources(), bitmap));
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment