这是我的 Gallery 的适配器,我在上面显示 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;
}
这行得通,但是如果我拖动图库, View (当切换到真实位图时)会“跳转”到厨房内的另一个位置,这很奇怪。
我怎样才能避免这种情况?或者我可以在不请求 ViewGroup 布局的情况下更改 ImageView 的图像吗?
编辑:谷歌开发组的类似问题 http://code.google.com/p/android/issues/detail?id=15526
最佳答案
Gallery 小部件有一个已知错误,导致它总是返回一个空的 convertView。这意味着每次调用 getView 时,您都会创建一个新的 ImageView,这可能会导致您看到的问题。
有一个第 3 方创建的 Gallery 修复了回收错误,在线搜索 EcoGallery,您可以找到实现它的方法。当您滚动画廊时,它会显着提高图形性能。
关于android - 当从它的 child 那里请求无效时,画廊上的奇怪动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8527006/