gpt4 book ai didi

Android HTML ImageGetter 作为 AsyncTask

转载 作者:IT老高 更新时间:2023-10-28 21:44:53 25 4
gpt4 key购买 nike

好的,我正在为这个而失去理智。我的程序中有一个解析 HTML 的方法。我想包含内联图像,我的印象是使用 Html.fromHtml(string, Html.ImageGetter, Html.TagHandler) 将允许这种情况发生。

由于 Html.ImageGetter 没有实现,由我来编写。但是,由于将URL解析成Drawables需要网络访问,所以我不能在主线程上这样做,所以它必须是一个AsyncTask。我想。

但是,当您将 ImageGetter 作为参数传递给 Html.fromHtml 时,它使用的 getDrawable 方法必须被覆盖。因此无法调用触发 doInBackground 方法的整个 ImageGetter.execute 交易,因此无法真正实现异步。

我是不是完全错了,或者更糟的是,这不可能吗?谢谢

最佳答案

我做了一些与你想做的事情非常相似(我认为)的事情。那时我需要做的是解析 HTML 并将其设置回 TextView,我还需要使用 Html.ImageGetter 并且在主线程上获取图像时遇到同样的问题。

我基本做的步骤:

  • 为Drawable创建自己的子类,方便重绘,我称之为URLDrawable
  • 返回Html.ImageGetter
  • getDrawable方法中的 URLDrawable
  • 一旦调用 onPostExecute,我就重绘 Spanned 结果的容器

现在 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);
}
}
}

很简单,我只是重写了 draw,所以它会在 AsyncTask 完成后选择我在那里设置的 Drawable。

下面的类是Html.ImageGetter的实现,是从AsyncTask获取图片并更新图片的类

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];
return fetchDrawable(source);
}

@Override
protected void onPostExecute(Drawable result) {
// 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
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");
drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0
+ drawable.getIntrinsicHeight());
return drawable;
} catch (Exception e) {
return null;
}
}

private InputStream fetch(String urlString) throws MalformedURLException, IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);
return response.getEntity().getContent();
}
}
}

最后,下面是演示工作原理的示例程序:

String html = "Hello " +
"<img src='http://www.gravatar.com/avatar/" +
"f9dd8b16d54f483f22c0b7a7e3d840f9?s=32&d=identicon&r=PG'/>" +
" This is a test " +
"<img src='http://www.gravatar.com/avatar/a9317e7f0a78bb10a980cadd9dd035c9?s=32&d=identicon&r=PG'/>";

this.textView = (TextView)this.findViewById(R.id.textview);
URLImageParser p = new URLImageParser(textView, this);
Spanned htmlSpan = Html.fromHtml(html, p, null);
textView.setText(htmlSpan);

关于Android HTML ImageGetter 作为 AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7424512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com