当其子项请求无效时,图库上会出现奇怪的动画
这是我的图库的适配器,我在其中显示 ImageViews (页面的拇指)
我想异步加载图像(有时这可能来自网络),所以我执行了该代码:
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView image = convertView != null ? convertView : new ImageView(getContext());
final PageInfo page = getItem(position);
image.setBackgroundColor(Color.WHITE);
image.setLayoutParams(new Gallery.LayoutParams(96, 170));
new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... arg0) {
try {
File thumbnail = page.thumbnail();//Thumbnail download the image if not available
return BitmapFactory.decodeStream(new FileInputStream(thumbnail));
} catch (ApplicationException e) {
return null;
} catch (NetworkException e) {
return null;
}
}
protected void onPostExecute(Bitmap result) {
if (result == null)
return;
image.setImageBitmap(result);
};
}.executeOnExecutor(executor);
return image;
}
这有效,但如果我拖动图库视图(当切换到真实位图时)会“跳转”到厨房内的另一个位置,这使得它很奇怪。
我怎样才能避免这种情况?或者我可以更改 ImageView 的图像而不请求 ViewGroup 的布局吗?
编辑:关于 Google 开发组的类似问题 http://code.google.com/p/android/issues/detail ?id=15526
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Gallery 小部件有一个已知错误,导致它始终返回空的 ConvertView。这意味着每次调用 getView 时,您都会创建一个新的 ImageView,这可能会导致您所看到的问题。
有一个第三方创建的 Gallery 可以修复回收错误,在线搜索 EcoGallery 你可以找到如何实现它。当您滚动图库时,它将显着提高图形性能。
The Gallery widget has a known bug that causes it to always return a null convertView. This means that every time getView is called you are creating a new ImageView which might be causing the issue you're seeing.
There is a 3rd party created Gallery that Fixes the recycling bug, search around online for EcoGallery and you can find how to implement it. It will noticeably increase the graphics performance while you are scrolling the gallery.
如果你不能让它正常工作,我曾经做了一个可能有帮助的黑客。
在
doInBackground()
结束时,就在返回之前,执行以下操作:这将等待更新图像,直到您只显示一个视图(或您指定的任何数字,重点是您屏幕上没有任何部分视图),导致“跳转”未被注意到。如果您的问题还涉及跳转到图库中完全不同的位置,我建议您在加载之前检查您选择了哪个对象,加载后找到该对象所在的位置,然后调用
setSelection() 到该位置。
但首先尝试所有其他解决方案...
If you don't get it to work properly, I once made a hack that might help.
At the end of your
doInBackground()
, just before you return, do:This will wait with updating your image until you only display one view (or whatever number you specify, the point is you don't have any partial views on screen), causing the 'jump' to go by unnoticed. If your problems also involve jumps to a completely different position in the gallery, I recommend that you check which object you have selected before loading, and after loading you find what position this object is at, and call
setSelection()
to that position.But try all the other solutions first...
如果你还没有找到解决方案,这里是我的。
您需要 CustomGallery,它几乎是 1:1 的默认 Android Gallery。但是你不能扩展 Gallery,你必须将整个源代码复制到你的类中。您需要对 CustomAbsSpinner 和 CustomAdapterView 类执行相同的操作。所有这一切只改变一行代码......
在您的 CustomGallery 类中将 onLayout() 方法更改为:
这将导致仅在实际需要时调用
layout()
。画廊行为中还存在另一个已知错误,该错误会导致
notifyDataSetChanged()
上画廊跳转。如果您也遇到此错误,只需注释掉 CustomAdapterView 类中的一行:无论如何,好主题Marcos!感谢您引导我找到解决方案。
如果您需要自定义类的源代码,请告诉我。
编辑(作者:Marcos Vasconcelos):
为了获得这些类的 R 修复,需要复制一堆可样式的东西。
对于那些正在搜索 Android 2.2 源代码(没有 res 文件夹,如果有人找到它请通知我们)的人,可以在这里找到:
http://en.newinstance.it/2010/ 12/01/android-sdk-2-2_r2-sources/
对于 API 的最新版本,这可以通过 SDK Manager 完成,并将位于 /sources/android-
If you still haven't found a solution here is mine.
You need CustomGallery, which is almost 1:1 default Android Gallery. But you can't extend Gallery, you have to copy whole source code to your class. You will need to do the same with CustomAbsSpinner and CustomAdapterView classes. All this to change only one line of code...
In your CustomGallery class change onLayout() method to this:
This will cause that
layout()
is called only when actually needed.There was also another known bug in gallery behaviour, which causes gallery jumps on
notifyDataSetChanged()
. If you are having this bug as well just comment out one line in CustomAdapterView class:Anyway, good topic Marcos! Thank you for leading me to the solution.
If you need source of the custom classes let me know.
EDIT (by Marcos Vasconcelos):
There's a bunch of styleables things that need to be copy in order to get the R fix for those classes.
For those who are searching the Android 2.2 source code (without res folder, if anyone found it please notify us), this can be found here:
http://en.newinstance.it/2010/12/01/android-sdk-2-2_r2-sources/
For more recent versions of the API this can be done trough SDK Manager and will be located into /sources/android-
如果您(a)滚动非常慢,或者(b)不回收convertView,它是否有可能起作用?
我会冒险猜测它确实如此......
原因是当您从先前对 getItem() 的调用中回收 ImageView 时,您并没有取消关联的 AsyncTask。您将有多个任务更新同一个 ImageView,这可能是您所描述的行为。
Does it by any chance work if you (a) scroll really slowly, or (b) don't recycle the convertView?
I'll go out on a limb and guess that it does...
The reason being that when you recycle an ImageView from a previous call to getItem(), you are not cancelling the associated AsyncTask. You will have multiple tasks updating the same ImageView, which could be the behaviour you describe.
您可以使用 LazyList 的概念,它从互联网获取图像并临时放入本地缓存,这间接导致图库中的顺利导航,因为本地保存的图像
是链接 https://github.com/thest1/LazyList 或 http://androidsnips.blogspot.com /2010/08/lazy-loading-of-images-in-list-view-in.html
you can use the concept of LazyList which fetches image from internet and temporary put in local cache which indirectly leads to smooth navigation in Gallery as image saved locally
here is the link https://github.com/thest1/LazyList or http://androidsnips.blogspot.com/2010/08/lazy-loading-of-images-in-list-view-in.html
您可以在开始下载实际图像之前设置默认位图。这将确保空视图不会设置错误的位图。
You can set a default bitmap before starting download of the actual image. This will make sure that the empty views don't get set with the wrong bitmaps.