异步 Html.ImageGetter 用于在 TextView 中设置多个图像

发布于 2024-10-15 17:49:54 字数 603 浏览 2 评论 0原文

我正在编写一个应用程序,它获取 HTML 页面并解析它们以在屏幕上显示。具体来说,该应用程序从留言板中提取 HTML 并列出用户发布的帖子。

问题是帖子中的很多内容都是 标签中的图片,所以我需要编写一个 Html.ImageGetter 来处理图片的下载。

我的 textView.setText() 方法将如下所示:

myTextView.setText(Html.fromHtml(myText, new ImageGetter() {                 
        @Override
        public Drawable getDrawable(String source) {
        Drawable d;

        // Need to async download image here 

         return d;
        }
    }, null));

同步执行此操作很简单,但是是否有建议的方法来异步执行此操作,以便它不会锁定我的 UI 线程?我还想最终构建这些图像的缓存,但我想一旦异步下载出现,这将非常简单。

I'm writing an application that takes HTML pages and parses them to display on the screen. Specifically, this application pulls HTML from a message board and lists posts made by users.

The problem is that a lot of the content in posts are pictures in <img> tags, so I need to write a Html.ImageGetter to handle the downloading of the images.

My textView.setText() method will look like this:

myTextView.setText(Html.fromHtml(myText, new ImageGetter() {                 
        @Override
        public Drawable getDrawable(String source) {
        Drawable d;

        // Need to async download image here 

         return d;
        }
    }, null));

Doing this synchronously is trivial, but is there a suggested way to do this asynchronously so that it doesn't lock up my UI thread? I would also like to eventually build in caching of these images, but I imagine that would be pretty simple once the async downloading was there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北陌 2024-10-22 17:49:54

同步执行此操作很简单,但是是否有建议的方法可以异步执行此操作,以便它不会锁定我的 UI 线程?

这将是困难的,也许是不可能的。您必须立即归还一些东西。即使这是一个占位符,您也会面临下载完成后如何替换占位符的挑战。由于您无法访问渲染图像的 ImageView(如果有 ImageView),我不知道您将如何安排替换占位符。

Doing this synchronously is trivial, but is there a suggested way to do this asynchronously so that it doesn't lock up my UI thread?

That will be difficult, perhaps impossible. You have to return something immediately. Even if that is a placeholder, you then have the challenge of how to replace the placeholder once the download is complete. Since you lack access to the ImageView rendering the image (if there is an ImageView), I don't know how you would arrange to replace the placeholder.

独守阴晴ぅ圆缺 2024-10-22 17:49:54

我会使用 AsyncTaskThread/Handler 组合。

莫言歌 2024-10-22 17:49:54

如何在显示 html 之前使用不可见的 Html.fromHtml 提取所有 url,将这些 url 放入列表中,异步下载所有图像,将它们存储在内存缓存中,然后实现 ImageGetter 以使用以下命令进行缓存查找: url 作为键,drawable 作为值。

How about extract all the urls before displaying the html by using an invisible Html.fromHtml, putting those urls in a list, asynchronously downloading all images, storing them in an in memory cache, then implementing the ImageGetter to just do a cache lookup using the url as a key and the drawable as the value.

泪冰清 2024-10-22 17:49:54

为了异步下载图像并将它们存储在内存缓存中,这里有一个迷你库:)
https://github.com/koush/UrlImageViewHelper

for downloading asynchronously the images and storing them in memory cache there is a mini-library here :)
https://github.com/koush/UrlImageViewHelper

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文