我正在尝试在textview中显示带有一些图像的html内容。问题是图像是重叠文本。这是代码。任何人都可以帮助我。
这里htmldata是包含html数据的字符串。
String htmldata="<here html data comes>";//html data comes from ws
URLImageParser p = new URLImageParser(tv_body, this);
Spanned htmlSpan = Html.fromHtml(htmldata, p, null);
tv_body.setText(htmlSpan);
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);
}
}
}
public class URLImageParser implements ImageGetter {
Context c;
View container;
/***
* Construct the URLImageParser which will execute AsyncTask and refresh the container
* @param t
* @param c
*/
public URLImageParser(View t, Context c) {
this.c = c;
this.container = t;
}
public Drawable getDrawable(String source) {
URLDrawable urlDrawable = new URLDrawable();
// get the actual source
ImageGetterAsyncTask asyncTask =
new ImageGetterAsyncTask( urlDrawable);
asyncTask.execute(source);
// return reference to URLDrawable where I will change with actual image from
// the src tag
return urlDrawable;
}
public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
URLDrawable urlDrawable;
public ImageGetterAsyncTask(URLDrawable d) {
this.urlDrawable = d;
}
@Override
protected Drawable doInBackground(String... params) {
String source = params[0];
System.out.println("source==="+source);
return fetchDrawable(source);
}
@Override
protected void onPostExecute(Drawable result) {
// set the correct bound according to the result from HTTP call
// System.out.println("result.getIntrinsicWidth()==="+result.getIntrinsicWidth());
// System.out.println("result.getIntrinsicHeight()==="+result.getIntrinsicHeight());
if(result!=null)
{
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
URLImageParser.this.container.invalidate();
}
/***
* Get the Drawable from URL
* @param urlString
* @return
*/
public Drawable fetchDrawable(String urlString) {
try {
InputStream is = fetch(urlString);
Drawable drawable = Drawable.createFromStream(is, "src");
if(drawable!=null){
drawable.setBounds(0,0, 0 + drawable.getIntrinsicWidth(), 0
+ drawable.getIntrinsicHeight());
}
else
{
drawable=null;
}
return drawable;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
答案 0 :(得分:0)
protected void onPostExecute(Drawable result) {
if(result!=null)
{
urlDrawable.setBounds(0, 0, 0 + result.getIntrinsicWidth(), 0
+ result.getIntrinsicHeight());
}
urlDrawable.drawable = result;
URLImageParser.this.container.invalidate();
//add this
URLImageParser.this.container.setText(URLImageParser.this.container.getText());
}