gpt4 book ai didi

android - HttpResponseCache 的 Gridview 性能

转载 作者:行者123 更新时间:2023-11-30 03:50:47 25 4
gpt4 key购买 nike

我正在使用 gridview 显示数百张图像(甚至可能是几千张)。图片位于服务器上,我正在使用 HttpResponseCache 缓存图片.我遇到的问题是,当我向下滑动 gridview 时,回收的 View 在最终确定正确的图像之前,每个 subview 显示 3 个或更多图像。这似乎是回调方法返回所有请求图像的结果。我怎样才能让 gridview 在向上/向下滚动时没有这种巨大的 Activity 旋风。

我的自定义适配器的 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;
}

}

最佳答案

我遇到的部分问题是由于未优化的 BaseAdapter.GetView

此外,当用户发起滑动手势时,我仍在尝试在 View 经过时加载所有图像。

This article !为我所犯的每个错误提供了详细的描述和解决方案。在那篇文章中还有一个源代码链接,该链接提供了一种在滑动手势完成之前停止加载图像的方法。

关于android - HttpResponseCache 的 Gridview 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14177227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com