使用 TextView 和 Html.ImageGetter 在 Android 上异步显示图像? [英] Display images on Android using TextView and Html.ImageGetter asynchronously?

查看:28
本文介绍了使用 TextView 和 Html.ImageGetter 在 Android 上异步显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下方法中的 SpannableString 设置 TextView:

I want to set a TextView with SpannableString which is from the method below:

Html.fromHtml(String source, Html.ImageGetter imageGetter, 
   Html.TagHandler tagHandler)

但是这里的ImageGetter需要重写下面的方法:

But the ImageGetter here need to override the method below:

public abstract Drawable getDrawable(String source)

因为我需要从互联网上获取 drawable,所以我必须异步进行,但似乎不是.

Because I need to get the drawable from the internet, I have to do it asynchronously and seems it is not.

如何让它工作?谢谢.

推荐答案

现在我使用 AsyncTask 下载 ImageGetter 中的图像:

Now I'm using an AsyncTask to download the images in the ImageGetter:

Spanned spannedContent = Html.fromHtml(htmlString, new ImageGetter() {

        @Override
        public Drawable getDrawable(String source) {
            new ImageDownloadAsyncTask().execute(textView, htmlString, source);
            return null;
        }
    }, null);

下载图片后,再次将文本设置到TextView中.

And set the text again into the TextView when the image has been downloaded.

现在可以了.但是当我尝试执行 TextView.postInvalidate() 重绘下载的图像时它失败了.我必须在 AsyncTask 中再次执行 setText().

Now it works. But It failed when I tried to do the TextView.postInvalidate() to redraw the downloaded images. I have to do setText() again in the AsyncTask.

有人知道为什么吗?

这篇关于使用 TextView 和 Html.ImageGetter 在 Android 上异步显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆