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

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

问题描述

我想用 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)

因为我需要从互联网上获取可绘制对象,所以我必须异步进行,似乎不是.

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() 来重绘下载的图像时它失败了.我必须在 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.

有人知道为什么吗?

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

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