安卓:延迟加载在画廊 [英] Android: lazy loading in Gallery

查看:130
本文介绍了安卓:延迟加载在画廊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我回顾了延迟加载一些职位,但我相信我的问题是有点不同。

I've reviewed some posts about lazy loading but I believe my problem is a bit different.

我有一个画廊(我的类扩展库),它会显示20大小的图像(400-500K每个)相当大。我不能加载他们全部图库,因为我得到一个内存不足的异常。
所以,我创建了20可绘制的数组,并开始填充前9个元素(图片来自网络),并设置所有,其余为空。我的目的是这样的:在一个一扔向右,取件没有。 10并设置为空元素没有。 0在另一个一扔向右取件没有。 11并设置为空元素没有。 1为空。上一扔同样的逻辑也离开了。

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.

现在的问题是,我可以扔的速度远远超过元素被取出。 我的画廊有一个BaseAdapter和getView()看起来是这样的:

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;
}

我如何告诉getView() - 如果imageArray [位置]尚空,显示正在加载...对话框,一旦它被设置重复自己使用相同的位置
我不希望看到的ImageView空,然后在飞行中进行设置。我想不能够看到在所有,直到它被设置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.

感谢。

推荐答案

画廊是专为流畅的使用体验。这将是非常糟糕的用户界面,如果你挡住屏幕,不切换到下一张图片,直到它被取出。这样用户就无法甩在所有。你应该展示一些负荷指标,而不是像在被装载。

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.

我觉得你的情况是相当普遍的。你应该下载图片,并显示出来。如果你的内存溢出,你可以尝试超级采样图像<一href="http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966">http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966.

I think your scenario is rather common. You should download images and display them. If you get OutOfMemory you can try to supersample images http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966.

如果仍有内存溢出,你应该从内存中删除位图,并将其缓存到SD。因此,当用户甩回来,你可以再次从SD载入图像,这将是速度不够快。和内存占用会更低。正如你建议你可以在内存中缓存最近10个图像和其他缓存在SD。

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.

您可以看看我的例子code <一个href="http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/3068012#3068012">http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/3068012#3068012.其实这是一个ListView适配器,但您可以将其应用到库稍作修改。我认为这将不正是你所需要的。

You can take a look at my sample code http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/3068012#3068012. 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.

这篇关于安卓:延迟加载在画廊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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