Android:图库中的延迟加载
我已经查看了一些有关延迟加载的帖子,但我相信我的问题有点不同。
我有一个画廊(我的班级扩展了画廊),其中显示 20 个相当大的图像(每个 400-500K)。由于出现 OutOfMemory 异常,我无法将它们全部加载到图库中。
因此,我创建了一个包含 20 个 Drawable 的数组,并最初填充了前 9 个元素(图像来自 Web),并将其余所有元素设置为 null。我的意图是:向右滑动时,获取第 1 号元素。 10 并设置为空元素号。 0. 在另一次向右滑动时,获取元素号。 11 并设置为空元素号。 1 为空。向左猛冲也是同样的逻辑。
问题是我的投掷速度比获取元素的速度快得多。 我的画廊有一个 BaseAdapter ,它的 getView() 看起来像这样:
public View getView(int position, View convertView, ViewGroup parent){ ImageView imageView = new ImageView(); imageView.setDrawable(imageArray[position]; .... .... return imageView; }
How do I Tell getView() - if imageArray[position] is still null, show a “loading...”dialog and一旦设置完毕,请重复使用相同职位?
我不想看到 imageView 为空然后动态设置。我希望在设置之前根本无法看到 imageView。
谢谢。
I've reviewed some posts about lazy loading but I believe my problem is a bit different.
I have a gallery (my class extends Gallery) which displays 20 rather large in size images (400-500K each). I cannot load them all to gallery since I get an OutOfMemory exception.
So, I created an array of 20 Drawables and initially populated the first 9 elements (the images come from the Web) and set all the rest to null. My intention was this: on a fling to the right, fetch element no. 10 and set to null element no. 0. On another fling to the right fetch element no. 11 and set to null element no. 1 to null. Same logic on a fling left.
The problem is I can fling much faster than the elements are fetched.
My gallery has a BaseAdapter and its getView() looks something like this:
public View getView(int position, View convertView, ViewGroup parent){ ImageView imageView = new ImageView(); imageView.setDrawable(imageArray[position]; .... .... return imageView; }
How do I tell getView() - if imageArray[position] is still null, show a "loading..." dialog and once it is set repeat yourself with the same position?
I don't want to see the imageView empty and then set on the fly. I want to not be able to see the imageView at all until it is set.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Gallery 专为流畅体验而设计。如果你挡住屏幕并且在获取下一个图像之前不切换到下一个图像,那么用户界面将非常糟糕。这样用户将根本无法投掷。您应该在加载时显示一些加载指示器而不是图像。
我认为你的情况很常见。您应该下载图像并显示它们。如果出现 OutOfMemory,您可以尝试对图像进行超级采样奇怪的内存不足问题,而将图像加载到 Bitmap 对象。
如果仍然存在 OutOfMemory,您应该从内存中删除位图并将它们缓存到 SD。因此,当用户向后滑动时,您可以再次从 SD 加载图像,速度会足够快。而且内存消耗会更低。正如您所建议的,您可以将 10 张最新图像缓存在内存中,将其他图像缓存在 SD 上。
您可以看一下我的示例代码 ListView 中的图像延迟加载。实际上它是一个 ListView 适配器,但您可以通过较小的修改将其应用到画廊。我认为它会完全满足您的需要。
Gallery is designed for smooth experience. It will be very bad UI if you block the screen and don't switch to next image until it is fetched. This way user will not be able to fling at all. You should display some loading indicator instead of image while it is loading.
I think your scenario is rather common. You should download images and display them. If you get OutOfMemory you can try to supersample images Strange out of memory issue while loading an image to a Bitmap object.
If there's still OutOfMemory you should remove bitmaps from memory and cache them to SD. So when user flings back you can load images from SD again, it will be fast enough. And memory consumption will be lower. As you suggest you can have 10 most recent images cached in memory and others cached on SD.
You can take a look at my sample code Lazy load of images in ListView. Actually it's a ListView adapter but you can apply it to gallery with minor modifications. I think it will do exactly what you need.