android Html img 标签解析
来源:互联网 发布:哈弗红标 2.0 知乎 编辑:程序博客网 时间:2024/05/22 10:32
Html.fromHtml(url,imageGetter,TagHandler)重写ImageGetter 异步加载图片,加载图片之后重置TextView(EditView)的内容。 参数说明:source=需要展示的html文本内容,imageGetter=需要继承Html.ImageGetter接口实现逻辑,tagHandler=这个参数表示,当textView解析遇到无法识别的html标签是否发送通知或者消息,如果遇到无法解析的标签,该方法将会被调用。这个没有具体测试过,一般赋值null。 问题:1网络图片的加载不能阻塞主线程,因此需要异步加载 (ImageLoader 会好些 有缓存机制) 2加载图片之后无法正常显示(偏小) (使用Drawable.createFromResourceStream) 3重新设置图片大小之后在个别系统上会出现图片错位(这个暂时无法解决,还望有解决办法的共享一下解决思路)(没复现)代码: UrlDrawable :可以让你加载图片的时候显示初始的图片,也就是加载中的图片。
public class URLDrawable extends BitmapDrawable { // the drawable that you need to set, you could set the initial drawing // with the loading image if you need to protected Drawable drawable; @Override public void draw(Canvas canvas) { // override the draw to facilitate refresh function later if(drawable != null) { drawable.draw(canvas); } }}
UrlParser :重写ImageGetter.
public class URLImageParser implements Html.ImageGetter { Context context; EditText container; /*** * 构建URLImageParser将运行AsyncTask,刷新容器 * @param editText * @param c */ public URLImageParser(EditText editText, Context c) { this.context = c; this.container = editText; } public Drawable getDrawable(String source) { URLDrawable urlDrawable = new URLDrawable(); //TODO ImageLoader // 获得实际的源 ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask( urlDrawable); asyncTask.execute(source); //返回引用URLDrawable将改变从src与实际图像标记 return urlDrawable; } public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> { URLDrawable urlDrawable; public ImageGetterAsyncTask(URLDrawable drawable) { this.urlDrawable = drawable; } @Override protected Drawable doInBackground(String... params) { String source = params[0]; return fetchDrawable(source); } @Override protected void onPostExecute(Drawable result) { // 设置正确的绑定依据HTTP调用的结果 if(result != null){ urlDrawable.setBounds(0, 0, result.getIntrinsicWidth(), result.getIntrinsicHeight()); urlDrawable.drawable = result; // 绘制图像容器 URLImageParser.this.container.invalidate(); URLImageParser.this.container.setHeight(URLImageParser.this.container.getHeight() + result.getIntrinsicHeight()); URLImageParser.this.container.setEllipsize(null); } } /*** * 得到Drawable的URL * * @param urlString * @return */ public Drawable fetchDrawable(String urlString) { try { URL url = new URL(urlString); //使用Drawable.createFromResourceStream可以使图片按原图大小展示出来不会出现加载后图片变小的情况 ,使用Drawable.createFromStream会。 Drawable drawable = Drawable.createFromResourceStream(context.getResources() , null ,url.openStream(), "src", null); if(drawable!= null){ drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); return drawable; }else{ return null; } } catch (Exception e) { return null; } } }}
使用textView加载带网络图片的html内容需求是可以实现的,但是由于html支持的标签有限,自己实现会很复杂,例如自定义字体颜色、span标签等都不能很好的支持,所以选择了webview展示,相对于EditView来说,不可编辑但是展示效果和处理比EditView的效果好,不需要自己写需要解析的标签(坑)。图文并茂的页面还是使用webView比较好,但是webView是不是就完美解决了呢?然而并不是,毕竟webView加载页面是页面的内容,效果没有Android原生的好,同事使用webView加载页面在Android系统4.4+开始滑动页面会有明显的卡顿和跳帧,有些简直无法接受。使用腾讯X5SDK优化webView加载腾讯X5SDK能够加速webView加载,优化滑动卡顿,效果还是比较明显的(没测),只是使用条件比较苛刻,在国内使用的APP还是可以考虑。实现图文混排的另一种方式,不是对img标签1.spanString (http://blog.csdn.net/feizhixuan46789/article/details/10334441)
0 0
- android Html img 标签解析
- html之img标签
- HTML <img> 标签
- HTML <img> 标签
- HTML-认识<img>标签
- 利用Jsoup解析html,如何得到<img>标签的位置
- Jsoup-Java解析Html类库,替换img标签style样式
- HTML<a>标签,<img>标签
- HTML标签<img><map><area>
- HTML img标签align属性
- HTML <img>标签路径问题
- HTML标签img--改变图片尺寸
- 前端html之img标签
- 3-HTML中的img标签
- html--图像显示<img>标签
- HTML(五) 使用img标签
- HTML的<img>标签基础
- html img标签的使用
- JAVA设计模式之观察者模式
- mysql去重
- 目标分解总结
- 【分布式数据库】【相关】
- Codeforces Round #392 (Div. 2) A(水)B(水、模拟) C(数学) D(贪心)
- android Html img 标签解析
- shell学习总结
- 安卓内存分析实例
- python中json.dumps
- Android常见错误
- JSONObject、JSONArray方法使用详解
- Un-sorted list, find the "k" largest value, the original list can be changed
- maven pom.xml详解
- php跨域问题 跨域上传图片 手机端上传图片 省市区联动 转64位字符串上传图片