Skip to content

Instantly share code, notes, and snippets.

@extralam
Created April 12, 2017 02:53
Show Gist options
  • Save extralam/017900f6eb616e9ae97eec9904dd90a6 to your computer and use it in GitHub Desktop.
Save extralam/017900f6eb616e9ae97eec9904dd90a6 to your computer and use it in GitHub Desktop.
URLImageParser - TextView show Image html , use Glide Image Library
import java.util.ArrayList;
import com.bumptech.glide.GenericRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html.ImageGetter;
import android.widget.TextView;
/**
* Idea From http://www.jianshu.com/p/037ae1dfb442
*
**/
public class URLImageParser implements ImageGetter{
ArrayList<Target> targets;
final TextView mTextView;
Context mContext;
public URLImageParser(Context ctx , TextView tv){
this.mTextView = tv;
this.mContext = ctx;
this.targets = new ArrayList<Target>();
}
@Override
public Drawable getDrawable(String url) {
final UrlDrawable urlDrawable = new UrlDrawable();
final GenericRequestBuilder load = Glide.with(mContext).load(url).asBitmap();
final Target target = new BitmapTarget(urlDrawable);
targets.add(target);
load.into(target);
return urlDrawable;
}
private class BitmapTarget extends SimpleTarget<Bitmap> {
Drawable drawable;
private final UrlDrawable urlDrawable;
public BitmapTarget(UrlDrawable urlDrawable) {
this.urlDrawable = urlDrawable;
}
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
drawable = new BitmapDrawable(mContext.getResources(), resource);
mTextView.post(new Runnable() {
@Override
public void run() {
int w = mTextView.getWidth();
int hh=drawable.getIntrinsicHeight();
int ww=drawable.getIntrinsicWidth() ;
int newHeight = hh * ( w )/ww;
Rect rect = new Rect( 0 , 0 , w ,newHeight);
drawable.setBounds(rect);
urlDrawable.setBounds(rect);
urlDrawable.setDrawable(drawable);
mTextView.setText(mTextView.getText());
mTextView.invalidate();
}
});
}
}
class UrlDrawable extends BitmapDrawable{
private Drawable drawable;
@SuppressWarnings("deprecation")
public UrlDrawable() {
}
@Override
public void draw(Canvas canvas) {
if (drawable != null)
drawable.draw(canvas);
}
public Drawable getDrawable() {
return drawable;
}
public void setDrawable(Drawable drawable) {
this.drawable = drawable;
}
}
}
@namsa87
Copy link

namsa87 commented Mar 21, 2019

Hi. I looked at the source code you wrote.
Thanks to that, I came to the desired result.
However, you can not use GIF for that source. How do I use GIF?

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