1.自定义HtmlTextView
import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.view.MotionEvent;
import com.orhanobut.logger.Logger;
import com.sito.library.utils.LocalImageGetter;
import com.sito.library.utils.UrlImageGetter;
import org.xml.sax.XMLReader;
/**
* Created by zhangan on 2017-06-15.
*/
public class HtmlTextView extends android.support.v7.widget.AppCompatTextView {
public static final String TAG = "HtmlTextView";
public static final boolean DEBUG = false;
public HtmlTextView(Context context) {
super(context);
}
public HtmlTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public void setHtmlFromString(String html, boolean useLocalDrawables,String host,String cook) {
Html.ImageGetter imgGetter;
if (useLocalDrawables) {
imgGetter = new LocalImageGetter(getContext());
} else {
imgGetter = new UrlImageGetter(getContext(), this, TextUtils.isEmpty(host)?"":host,TextUtils.isEmpty(cook)?"":cook);
}
try {
setText(Html.fromHtml(html, imgGetter, new MTagHandler()));
setMovementMethod(LinkMovementMethod.getInstance());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return false;
}
Html.TagHandler tagHandler = new Html.TagHandler() {
boolean first = true;
String parent = null;
int index = 1;
@Override
public void handleTag(boolean opening, String tag, Editable output,
XMLReader xmlReader) {
Logger.d(tag + " output " + output + "opening " + opening);
if (tag.equals("ul")) parent = "ul";
else if (tag.equals("ol")) parent = "ol";
if (tag.equals("li")) {
if (parent.equals("ul")) {
if (first) {
output.append("\n\t•");
first = false;
} else {
first = true;
}
} else {
if (first) {
output.append("\n\t" + index + ". ", 0, output.length() + 1);
first = false;
index++;
} else {
first = true;
}
}
}
}
};
}
2.LocalImageGetter类
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;
public class LocalImageGetter implements Html.ImageGetter {
Context c;
public LocalImageGetter(Context c) {
this.c = c;
}
@Override
public Drawable getDrawable(String source) {
int id = c.getResources().getIdentifier(source, "drawable", c.getPackageName());
if (id == 0) {
id = c.getResources().getIdentifier(source, "drawable", "android");
}
if (id == 0) {
return null;
} else {
Drawable d = c.getResources().getDrawable(id);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
}
}
3.UrlImageGetter类
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
/**
* 图文混排
* Created by zhangan on 2017-06-15.
*/
public class UrlImageGetter implements Html.ImageGetter {
private Context c;
private TextView container;
private int width;
private String host;
private String cook;
public UrlImageGetter(Context c, TextView container,String host,String cookiestr) {
this.c = c;
this.container = container;
width = c.getResources().getDisplayMetrics().widthPixels;
this.host = host;
this.cook = cookiestr;
}
@Override
public Drawable getDrawable(String source) {
final UrlDrawable urlDrawable = new UrlDrawable();
GlideUrl cookie = new GlideUrl(source.startsWith("http")?source:host+source, new LazyHeaders.Builder().addHeader("Cookie", cook).build());
Glide.with(c).load(cookie).asBitmap().into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap loadedImage, GlideAnimation<? super Bitmap> glideAnimation) {
float scaleWidth = ((float) width) / loadedImage.getWidth();
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleWidth);
loadedImage = Bitmap.createBitmap(loadedImage, 0, 0, loadedImage.getWidth(), loadedImage.getHeight(), matrix,
true);
urlDrawable.bitmap = loadedImage;
urlDrawable.setBounds(0, 0, loadedImage.getWidth(), loadedImage.getHeight());
container.invalidate();
container.setText(container.getText());
}
});
return urlDrawable;
}
}
4.用法
布局中
<com.xxx.widget.HtmlTextView
android:id="@+id/id_recase_content"
android:layout_marginTop="@dimen/dp_10"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:text="内容"
android:textColor="@color/list_content"
android:textSize="14sp"/>
页面代码
找到自定义图文混排控件‘
mContent.setHtmlFromString(caseInfo.getCaseContent(),false, Api.HOST, Api.getCookieStr())