Android TextView Html ImageGetter 图片位置不对
来源:互联网 发布:我的危险妻子 知乎 编辑:程序博客网 时间:2024/05/13 04:21
TextView 显示Html的方法:
textView.setText(Html.fromHtml(string))
Tags supported by Android
<p>
<div>
handled exactly like<p>
<br>
<b>
<i>
<strong>
<em>
<u>
<tt>
<dfn>
<sub>
<sup>
<blockquote>
<cite>
<big>
<small>
<font size="..." color="..." face="...">
<h1>
,<h2>
,<h3>
,<h4>
,<h5>
,<h6>
<a href="...">
<img src="...">
推荐一个开源项目,支持Html的TextView,添加了几个Tags的支持:
https://github.com/sufficientlysecure/html-textview
Extended support by HtmlTextView
<ul>
<ol>
<li>
<code>
<center>
<strike>
但是显示图片位置不对,相信很多人遇到这个问题。
TextView显示图片都要自己写个ImageGetter的类,如果图片位置不对,可以试试这行代码,在onPostExecute里面加上
((HtmlTextView)container).setText(((HtmlTextView) container).getText());
完整类:
package org.sufficientlysecure.htmltextview;import android.content.Context;import android.content.Intent;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.os.AsyncTask;import android.text.Html.ImageGetter;import android.util.Log;import android.view.View;import com.example.administrator.bbcodetest.R;import java.io.IOException;import java.io.InputStream;import java.net.URI;import java.net.URL;public class HtmlRemoteImageGetter implements ImageGetter { Context c; View container; URI baseUri; /** * Construct the URLImageParser which will execute AsyncTask and refresh the container */ public HtmlRemoteImageGetter(View t, Context c, String baseUrl) { this.c = c; this.container = t; if (baseUrl != null) { this.baseUri = URI.create(baseUrl); } } public Drawable getDrawable(String source) { UrlDrawable urlDrawable = new UrlDrawable(); ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask(urlDrawable); asyncTask.execute(source); // return reference to URLDrawable which will asynchronously load the image specified in the src tag return urlDrawable; } public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> { UrlDrawable urlDrawable; String source; public ImageGetterAsyncTask(UrlDrawable d) { this.urlDrawable = d; } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onPostExecute(final Drawable result) { if (result == null) { Log.w(HtmlTextView.TAG, "Drawable result is null! (source: " + source + ")"); return; } // set the correct bound according to the result from HTTP call// urlDrawable.setBounds(0, 0, 0 + result.getIntrinsicWidth(), 0 + result.getIntrinsicHeight()); // change the reference of the current drawable to the result from the HTTP call urlDrawable.drawable = result; // redraw the image by invalidating the container urlDrawable.invalidateSelf(); HtmlRemoteImageGetter.this.container.invalidate(); // important <span style="color:#ff0000;">((HtmlTextView)container).setText(((HtmlTextView) container).getText());</span> }@Override protected Drawable doInBackground(String... params) { source = params[0]; return fetchDrawable(source); } /** * Get the Drawable from URL */ public Drawable fetchDrawable(String urlString) { try { InputStream is = fetch(urlString); Bitmap bitmap= BitmapFactory.decodeStream(is); Drawable drawable = new BitmapDrawable(c.getResources(), bitmap); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int newWidth = width; int newHeight = height; if( width > container.getWidth() ) { newWidth = container.getWidth(); newHeight = (newWidth * height) / width; } drawable.setBounds(0, 0, 0 + newWidth, 0 + newHeight); urlDrawable.setBounds(0, 0, newWidth, newHeight); return drawable; } catch (Exception e) { return null; } } private InputStream fetch(String urlString) throws IOException { URL url; if (baseUri != null) { url = baseUri.resolve(urlString).toURL(); } else { url = URI.create(urlString).toURL(); } return (InputStream) url.getContent(); } } @SuppressWarnings("deprecation") public class UrlDrawable extends BitmapDrawable { protected Drawable drawable; @Override public void draw(Canvas canvas) { // override the draw to facilitate refresh function later if (drawable != null) { drawable.draw(canvas); } } }}
0 0
- Android TextView Html ImageGetter 图片位置不对
- Html.ImageGetter() textview加载图片 出现[obj]的问题
- 重写ImageGetter,让TextView异步加载包含图片的html内容
- Android中类似微博论坛中用ImageGetter异Html图片
- [Android]Html.from()中ImageGetter异步加载并显示图片和替换标签处理效果
- Html类ImageGetter接口
- Html类ImageGetter接口
- 如何实现Html.fromhtml随着Imagegetter在Android中
- 如何实现Html.fromhtml随着Imagegetter在Android中
- android 图文混排,显示网络图片(续) ImageGetter
- Android TextView中显示HTML和图片
- Android TextView 显示HTML加图片
- Android textview获取html文字 + 图片显示
- Android TextView加载html图片详解
- Android TextView Html加载图片Glide
- Android ImageGetter接口的使用
- Android控件TextView显示html文件中的图片
- android的TextView使用Html(图片显示)格式。。。。。。
- maven的依赖分析管理
- 绝对让你理解Android中的Context
- 关于android 使用bitmap的OOM心得和解决方案
- linuxshell 求列和
- 博聊论坛:每日会员免费体育竞猜有奖活动(7月20日第八十四期)
- Android TextView Html ImageGetter 图片位置不对
- 在Django 模板中使用session
- POJ 2420 A Star not a Tree?
- 类,封装,继承,访问修饰符
- (1.5.2.0)编程之美思路总结
- OC语言字符串的多种方法
- 【回文串】 Jisuanke 商品推荐走马灯(中等)
- 动态加载xml文件
- day05