在后台线程或 UI 线程中加载图像?

Load images in background thread or UI thread?

我的 android 应用程序有一个 ListView,它在后台从网络中获取一组图像 url,然后将其传递给 UI 线程。 UI 线程然后下载图像并显示它们(我使用 Picasso 来确保缓存和调整大小。)

我想知道是否应该在后台线程中下载图像,然后仅将调整大小的位图传递给 UI 线程进行显示。此外,当用户在这两种情况下滚动屏幕时会发生什么情况 - 是否会再次从网络上检索所有图像?我无法理解在这种情况下哪个会更快。该应用程序与 Facebook android 应用程序非常相似。

还有一件事 - 我的应用程序每 5 分钟刷新一次,如果列表中有任何新图像 url,它会从后台线程开始重新启动整个进程。我的想法是,由于较早的图像会被缓存,因此不会再次加载它们。只会加载新的 urls。这是正确的吗?

Android 所有网络操作都在后台运行,Picasso 也不例外。当你滚动你的列表时,Picasso 会检查它的缓存,如果缓存中没有特定的图像,它就会下载它。

The UI thread then downloads the images and displays them (I'm using Picasso for the same to ensure caching and resizing.)

Picasso 默认不在主应用程序线程上下载图像。我什至不确定是否有这样做的选项。

I would like to know if I should download the images in the background thread and then pass only the resized bitmaps to the UI thread for display.

毕加索应该为你做这件事。

Also, what happens when the user scrolls the screen in both the scenarios - do all images get retrieved from the web again?

这取决于图片的数量、图片的大小、缓存的大小等等。 Picasso 为您提供了确定其行为方式的选项,从角落的调试指示器指示图像源 (setIndicatorsEnabled(true)) 到获取 StatsSnapshot 告诉您缓存的执行情况 (getSnapshot()).