Android中Textview显示Html,图文混排,支持图片点击放大
本文首发于网易云社区
对于呈现Html文本来说,Android提供的Webview控件可以得到很好的效果,但使用Webview控件的弊端是效率相对比较低,对于呈现简单的html文本的话,杀鸡不必使用牛刀。另外如果是在Listview中使用的Webview的话,效率则更是低下。
然而,Android还提供了android.text.Html类来支持Html的解析,利用这个类,我们可以通过Textview来呈现Html文件。不过Html类并不是只是所有的标签。Html的描述如下:
This class processes HTML strings into displayable styledtext.
Not all HTML tags are supported.
一、解析Html标签
对于纯文字的Html文本来说,使用Textview来呈现很方便,一行代码就可以轻松搞定了,具体如下:
contentTextView.setText(Html.formHtml(htmlString));
二、处理img图片标签,实现图文混排
一般来说,html文件常常是含有图片,如果需要在Textview中实现文字和图片的混排,需要使用ImageGetter。ImageGetter是Html类中一个接口,作用是给img标签获取图片内容,主要提供了一个getDrawable的方法。
/**
* Retrieves images for HTML <img> tags.
*/
public static interface ImageGetter {
/**
* This methos is called when the HTML parser encounters an
* <img> tag. The <code>source</code> argument is the
* string from the "src" attribute; the return value should be
* a Drawable representation of the image or <code>null</code>
* for a generic replacement image. Make sure you call
* setBounds() on your Drawable if it doesn't already have
* its bounds set.
*/
public Drawable getDrawable(String source);
}
解析来需要实现一个ImageGetter,具体如下:
public class MyImageGetter implements ImageGetter {
WeakReference<TextView> mTextViewReference;
Context mContext;
public MyImageGetter(Context context, TextView textView, int with) {
mContext = context.getApplicationContext();
mTextViewReference = new WeakReference<TextView>(textView);
}
@Override
public Drawable getDrawable(String url) {
URLDrawable urlDrawable = new URLDrawable(mContext);
// 异步获取图片,并刷新显示内容
new ImageGetterAsyncTask(url, urlDrawable).execute();
return urlDrawable;
}
public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
WeakReference<URLDrawable> mURLDrawableReference;
String mUrl;
public ImageGetterAsyncTask(String url, URLDrawable drawable) {
mURLDrawableReference = new WeakReference<URLDrawable>(drawable);
mUrl = url;
}
@Override
protected Drawable doInBackground(String... params) {
// 下载图片,并且使用缓存
Bitmap bitmap = DownlaodUtils.getNetworkImageWithCache(mContext, mUrl);
BitmapDrawable bitmapDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
Rect bounds = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
if (mURLDrawableReference.get() != null) {
mURLDrawableReference.get().setBounds(bounds);
}
bitmapDrawable.setBounds(bounds);
return bitmapDrawable;
}
@Override
protected void onPostExecute(Drawable result) {
if (null != result) {
if (mURLDrawableReference.get() != null) {
mURLDrawableReference.get().drawable = result;
}
if (mTextViewReference.get() != null) {
// 加载完一张图片之后刷新显示内容
mTextViewReference.get().setText(mTextViewReference.get().getText());
}
}
}
}
public class URLDrawable extends BitmapDrawable {
protected Drawable drawable;
public URLDrawable(Context context) {
// 设置默认大小和默认图片
Rect bounds = new Rect(0, 0, 100, 100);
setBounds(bounds);
drawable = context.getResources().getDrawable(R.drawable.default_image);
drawable.setBounds(bounds);
}
@Override
public void draw(Canvas canvas) {
if (drawable != null) {
drawable.draw(canvas);
}
}
}
}
实现了MyImageGetter之后,则需要实现图文混排就轻而易举了
MyImageGetter imageGetter = new MyImageGetter(this, contentTextView);
contentTextView.setText(Html.formHtml(htmlString, imageGetter, null));
三、图片的点击放大
前面已经实现了Textview呈现html文本,并且能够图文混排。但很多情况下,需要支持点击图片后将图片放大显示。这样,我们需要支持img标签的点击处理,能够监听到点击事件就可以实现这个功能了。这里我们可以通过实现TagHandler接口来实现这个功能。首先看下android.text.Html类中的Taghandler接口:
/**
* Is notified when HTML tags are encountered that the parser does
* not know how to interpret.
*/
public static interface TagHandler {
/**
* This method will be called whenn the HTML parser encounters
* a tag that it does not know how to interpret.
*/
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader);
}
我们只需实现TagHandler的handleTag方法来处理img标签则可,主要是给内容设置一个ClickableSpan,具体如下:
public class MyTagHandler implements TagHandler {
private mContext;
public MyTagHandler(Context context) {
mContext = context.getApplicationContext();
}
@Override
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
// 处理标签<img>
if (tag.toLowerCase(Locale.getDefault()).equals("img")) {
// 获取长度
int len = output.length();
// 获取图片地址
ImageSpan[] images = output.getSpans(len-1, len, ImageSpan.class);
String imgURL = images[0].getSource();
// 使图片可点击并监听点击事件
output.setSpan(new ClickableImage(mContext, imgURL), len-1, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
private class ClickableImage extends ClickableSpan {
private String url;
private Context context;
public ClickableImage(Context context, String url) {
this.context = context;
this.url = url;
}
@Override
public void onClick(View widget) {
// 进行图片点击之后的处理
}
}
}
实现了TagHandler之后,在formHtml传入实例则可:
MyImageGetter imageGetter = new MyImageGetter(this, contentTextView);
MyTagHandler tagHandler = new MyTagHandler(this);
contentTextView.setText(Html.formHtml(htmlString, imageGetter, tagHandler));
contentTextView.setMovementMethod(LinkMovementMethod.getInstance());
网易云产品免费体验馆,无套路试用,零成本体验云计算价值。
本文来自网易云社区,经作者戚明峰授权发布
相关文章:
【推荐】 Windows扩展屏开发总结
Android中Textview显示Html,图文混排,支持图片点击放大的更多相关文章
- TextView + Spanned实现图文混排以及图片点击交互
最近要实现图文混排的需求,webview过大,所以想到了用SpannableStringBuilder来实现. 不过参考了大量国内文章,大多数是教你如何实现图文混排,并没有提及图片点击交互的.有翻阅了 ...
- DIV+CSS 图文混排的图片居中办法
不少人为了让 Div 图文混排的图片可以居中,给 IMG 套各式各样的 SPAN.DIV.LI 等等,以便于使用 text-align来进行居中. <div>图文混排 <br> ...
- Unity插件之NGUI学习(5)—— 创建Label图文混排及文字点击
创建一个新的Scene,并按 Unity插件之NGUI学习(2)创建UI Root. 准备工作,制作Font.如今Project窗体创建一个Font目录.然后从系统自带字体目录中选择自己须要的字体,我 ...
- iOS 图文混排 链接 可点击
对于这个话题 我想到 1 第一个解决方法就是使用 webView 比较经典 把所有复杂工作都交给控件本身去处理了, 但是好像好多需要自定义的地方 没法从 webView获得响应回调 :(估计也可以实 ...
- 关于Android中TextView显示多个空格
一.直接填写文字,输入多少,显示多少,如下: android:text="AAA AAA" ————————>显示:AAA AAA 二.通过设置str ...
- [UGUI]图文混排(六):点击区域
点击区域可以分成两部分来分析: 0.Rect 搜索api:Rect和Rect.Rect,可以知道: 在GUI和GUILayout中,Rect的原点在左上角,向右为x轴正方向,向下为y轴正方向: 除此之 ...
- HTML5[8]: 图文混排,图片与文字居中对齐
<img src="image.png"><span>999</span> img { /* ... */ vertical-align: t ...
- 简单的Coretext 图文混排
在很多新闻类或有文字展示的应用中现在都会出现图文混排的界面例如网易新闻等,乍一看去相似一个网页,其实这样效果并非由UIWebView 加载网页实现.现在分享一种比较简单的实现方式 iOS sdk中为我 ...
- CoreText实现图文混排之文字环绕及点击算法
系列文章: CoreText实现图文混排:http://www.jianshu.com/p/6db3289fb05d CoreText实现图文混排之点击事件:http://www.jianshu.co ...
随机推荐
- probably another instance of uWSGI is running on the same address (127.0.0.1:9090). bind(): Address already in use
probably another instance of uWSGI is running on the same address (127.0.0.1:9090). bind(): Address ...
- 错误:HttpServlet was not found on the Java
我们在用Eclipse进行Java web开发时,可能会出现这样的错误:The superclass javax.servlet.http.HttpServlet was not found on t ...
- RAD C++Builder xe7 std::map xtree BUG
c++Builder 6 下的std::map还能用,有代码提示. 换到xe7,代码提示出来就一个tt.operator [](),代码没法往下写了. 最后把Target Platforms切换到64 ...
- VB中的正则表达式
RegExp对象提供简单的正则表达式支持功能. RegExp对象的用法:Function RegExpTest(patrn, strng)Dim regEx, Match, Matches ' 建立变 ...
- maven的pom报plugins缺失的解决方法
maven的pom报plugins却是的解决方法. 引用 Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom: ...
- CMD-SVN查看版本修改记录
[CMD-SVN查看版本修改记录] 问题:想查看某个版本的具体修做了哪些改动? 方法:svn diff -r r1:(r1-1) (filename) filename可选,如果加上就表示查看 ...
- ssh 连接很慢的解决办法
http://blog.csdn.net/ablo_zhou/article/details/5074887 ============= 现象: 在局域网内,能ping通目标机器,并且时延是微秒级. ...
- springboot中的任务(异步任务--定时任务--邮件任务)
1.pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- mysql查询赋值、修改拼接字符串
sql中修改字符串类型的字段可以这么拼接:update tbName set UserName='abc'+UserName; 但mysql中就不行了,需要这样:update tbName set U ...
- [IIS] 测试的产品登陆之后有个引用外部站点js的请求半天都无法返回,导致网页一直在打转,Selenium的driver也无法对页面进行下一步的操作
测试的产品登陆之后有个引用外部站点js的请求半天都无法返回: https://cdn.heapanalytics.com/js/heap-3497400264.js 这个js如果是在美国的机器上就可以 ...