Skip to content

Instantly share code, notes, and snippets.

@rajeefmk
Created April 28, 2017 17:27
Show Gist options
  • Save rajeefmk/beb1b79363c12041da7fd540bcf27765 to your computer and use it in GitHub Desktop.
Save rajeefmk/beb1b79363c12041da7fd540bcf27765 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) {
}
}
}
@9kl
Copy link

9kl commented Mar 29, 2020

App().get ...what ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment