使用的TextView和Html.ImageGetter异步Android上显示的图像?图像、Html、TextView、Android

2023-09-06 00:58:29 作者:迩長得欠抽

我想成立一​​个的TextView SpannableString 这是从下面的方法:

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)

由于我需要从互联网上获得的绘制,我也要异步做到这一点,看来事实并非如此。

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

如何使它工作? 谢谢你。

How to make it work? Thanks.

推荐答案

现在我使用的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()重绘下载的图像。我必须在的setText()再这样做了的AsyncTask

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.

有谁知道为什么吗?