@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
CharSequence c = (CharSequence) msg.getData().get("data");
TextView textView = (TextView) msg.obj;
textView.setText(c);
}
};
/**
* 加载html内容
*/
public void loadHtmlText(final TextView textView, final String html) {
textView.post(new Runnable() {
@Override
public void run() {
final int screenWidth = textView.getMeasuredWidth();
final Message msg = Message.obtain();
msg.obj = textView;
final Bundle bundle = new Bundle();
new Thread(new Runnable() {
@Override
public void run() {
Spanned spanned = Html.fromHtml(html, new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
InputStream is = null;
try {
is = (InputStream) new URL(source.replace("https", "http")).getContent();
Drawable dra = Drawable.createFromStream(is, "src");
int i = dra.getIntrinsicWidth() * 13;
int i1 = dra.getIntrinsicHeight() * 13;
dra.setBounds(0, 0, i, i1);
if (i > screenWidth & i1 != 0) {
float i2 = (float) i / i1;
dra.setBounds(0, 0, screenWidth, (int) (screenWidth / i2));
}
return dra;
} catch (MalformedURLException e) {
return null;
} catch (IOException e) {
return null;
} finally {
try {
if (null != is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}, null);
if (mHandler != null) {
bundle.putCharSequence("data", spanned);
msg.setData(bundle);
mHandler.sendMessage(msg);
}
}
}).start();
}
});
}
@Override
protected void onDestroy() {
if (mHandler != null){
mHandler = null;
}
super.onDestroy();
}