我正在使用gridview来显示数百张图片(甚至可能只有几千张)。图像位于服务器上,我使用HttpResponseCache缓存图像。我遇到的问题是,当我向下滑过gridview时,回收的视图在最终确定正确的图像之前,每个子视图显示3个或更多图像。它似乎是回调方法返回所有请求的图像的结果。当向上/向下滚动时,如何让gridview没有这种巨大的活动旋转。
我的自定义适配器的getView方法
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
v = li.inflate(R.layout.folder_button, null);
} else {
v = convertView;
}
TextView tv = (TextView)v.findViewById(R.id.tvFolderButtonTitle);
tv.setText(mBaseItems[position].Name);
tv.setTextColor(Color.WHITE);
ImageView iv = (ImageView)v.findViewById(R.id.ivFolderButtonImage);
iv.setLayoutParams(new LinearLayout.LayoutParams(folderWidth_, folderHeight_));
iv.setScaleType(ImageView.ScaleType.FIT_XY);
String imageUrl = "http://path.to.image";
api_.GetImageAsync(imageUrl, new GetImageStreamCallback(iv), false);
return v;
}
设置图像的回调方法。
public class GetImageStreamCallback implements IApiCallback {
private ImageView currentImageView;
public GetImageStreamCallback(ImageView imageView) {
currentImageView = imageView;
}
public void Execute(Object data) {
if (data != null) {
try {
Bitmap image = (Bitmap) data;
currentImageView.setImageBitmap(image);
} catch (Exception e) {
Log.i("Exception", "Error getting image");
}
}
}
}
从上面的api_.GetImageAsync调用自定义AsyncTask
public class AsyncRequestImage extends AsyncTask<String,String,Object > {
HttpURLConnection connection_;
InputStream inStream_;
IApiCallback callback_;
boolean ignoreCache_;
public AsyncRequestImage(IApiCallback callback, boolean ignoreCache) {
this.callback_ = callback;
this.ignoreCache_ = ignoreCache;
}
@Override
protected Object doInBackground(String... uri) {
Bitmap image;
if (ignoreCache_) {
image = acquireImage(uri[0], true);
} else {
image = acquireImage(uri[0], false);
if (image == null)
image = acquireImage(uri[0], true);
}
return image;
}
@Override
protected void onPostExecute(Object image) {
callback_.Execute(image);
}
private Bitmap acquireImage(String url, boolean ignoreCache) {
try {
URL _url = new URL(url);
connection_ = (HttpURLConnection) _url.openConnection();
connection_.addRequestProperty("Accept-Encoding", "gzip");
if (ignoreCache) {
connection_.setRequestProperty("Cache-Control", "max-age=0");
} else {
connection_.addRequestProperty("Cache-Control", "only-if-cached");
}
connection_.connect();
String encoding = connection_.getContentEncoding();
// Determine if the stream is compressed and uncompress it if needed.
if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
try {
inStream_ = new GZIPInputStream(connection_.getInputStream());
} catch (FileNotFoundException e) {
}
} else {
try {
inStream_ = connection_.getInputStream();
} catch (FileNotFoundException e) {
}
}
if (inStream_ != null) {
try {
Bitmap image = BitmapFactory.decodeStream(inStream_);
return image;
} catch (java.lang.OutOfMemoryError oom) {
FileLogger.getFileLogger().ReportInfo("UrlConnection: Bitmap creation failed. Out of memory");
}
}
} catch (IOException e) {
if (e != null && e.getMessage() != null) {
Log.i("AsyncRequestImage doInBackground:",e.getMessage());
}
} finally {
connection_.disconnect();
}
return null;
}
}
答案 0 :(得分:1)
我遇到的部分问题是由于未经优化的BaseAdapter.GetView
此外,当用户发起了一个投掷手势时,我仍然试图加载所有图像作为视图传递。
This article!为我所犯的每个错误提供了详细的描述和解决方案。此外,该文章还提供了一个源代码链接,该链接提供了一种方法,可以在fling手势完成之前停止加载图像。