TextView加载Html内容(自定义TextView)

背景:

有时需要在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格式的文本内容
 
 
 

 



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值