背景:
有时需要在Android的某个界面显示图文并茂的内容,而且文本内容与图片张数及其相对的位置不确定,那么很自然的会想到利用html格式来表达这样的一个图文并茂内容,可是在Android要怎么显示这个html形式的文本呢?
方案:
1.WebView:Html嘛,自然是WebView控件存在的意义,第一反应当然是它啦、
2.TextView:嘿嘿,其实Android在TextView这个控件里面定义的spanned属性也可以让TextView加载Html形式的文本哦。
代码:
现在我们不谈WebView谈TextView,下面自定义一个TextView,来完成显示Html文本的使命
上代码:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LevelListDrawable;
import android.os.AsyncTask;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.Spanned;
import android.util.AttributeSet;
import android.widget.TextView;
import com.freecats.mycustomviewlib.R;
import com.freecats.mycustomviewlib.util.ScreenUtil;
/**
* 对HTML文本进行特殊处理的TextView
*
*/
public class TextViewHtml extends TextView {
// 出处:http://stackoverflow.com/questions/26121657/showing-images-from-html-to-textview-android
/**
* 默认的占位图片资源
*/
private Drawable defaultDrawable;
private int width;
private int height;
public TextViewHtml(Context context) {
super(context);
}
public TextViewHtml(Context context, AttributeSet attr) {
super(context, attr);
//TypedArray ta = context.obtainStyledAttributes(attr,
// R.styleable.TextViewHtml);
//defaultDrawable = ta
// .getDrawable(R.styleable.TextViewHtml_defaultDrawable);//从xml文件中读取默认的占位图片
width = ScreenUtil.getScreenWidth(context);
height = ScreenUtil.getScreenHeight(context);
//ta.recycle();
}
/**
* <strong>主要方法<strong><br>
* 因为{@link TextView#setText(CharSequence)}是final,所以无法重写<br>
* 故在此提供一个{@link TextViewHtml#setHtmlText(CharSequence)}方法,接受传入的html形式的文本
*
* @param charSequence
*/
public void setHtmlText(CharSequence charSequence) {
Spanned spanned = Html.fromHtml(charSequence.toString(), imageGetter,
null);
setText(spanned);
}
public Drawable getDefaultDrawable() {
return defaultDrawable;
}
public void setDefaultDrawable(Drawable defaultDrawable) {
this.defaultDrawable = defaultDrawable;
}
public void setDefaultDrawableRes(int res) {
this.defaultDrawable = getResources().getDrawable(res);
}
private ImageGetter imageGetter = new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
LevelListDrawable d = new LevelListDrawable();
if (null == defaultDrawable) {
defaultDrawable = getResources().getDrawable(
R.drawable.ic_launcher);
}
Drawable empty = defaultDrawable;
d.addLevel(0, 0, empty);
d.setBounds(0, 0, empty.getIntrinsicWidth(),
empty.getIntrinsicHeight());
new LoadImage().execute(source, d);
return d;
}
};
class LoadImage extends AsyncTask<Object, Void, Bitmap> {
private LevelListDrawable mDrawable;
@Override
protected Bitmap doInBackground(Object... params) {
String source = (String) params[0];
mDrawable = (LevelListDrawable) params[1];
try {
InputStream is = new URL(source).openStream();
return BitmapFactory.decodeStream(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null) {
BitmapDrawable d = new BitmapDrawable(bitmap);
//根据屏幕的大小与图片的大小,对实际显示的图片宽高进行处理
int w = bitmap.getWidth() > width ? width : bitmap.getWidth();
int h = bitmap.getHeight() > height ? height : bitmap
.getHeight();
mDrawable.addLevel(1, 1, d);
mDrawable.setBounds(0, 0, w, h);
mDrawable.setLevel(1);
//直接invalidate并不会起到作用,所以使用setText来完成图片显示刷新
CharSequence t = TextViewHtml.this.getText();
TextViewHtml.this.setText(t);
// TextViewHtml.this.invalidate();
}
}
}
}
代码部分片段来自于StackOverFlow,本着封装主义精神,直接将其打包为一个自定义的TextView,在使用的时候一两句话了事:
<pre name="code" class="java">TextViewHtml textViewHtml = (TextViewHtml)findViewById(R.id.tv);
<pre name="code" class="java"><pre name="code" class="java">textViewHtml.setHtmlText(strHtml);//strHtml为html格式的文本内容