Android 具有HttpResponseCache的Gridview性能
我使用gridview显示数百张图像(甚至可能几千张)。图像位于服务器上,我正在使用缓存图像。我遇到的问题是,当我在gridview中向下滑动时,在最终确定正确的图像之前,每个子视图的回收视图显示3个或更多图像。这似乎是回调方法返回所有请求的图像的结果。如何使gridview在上/下滚动时不出现这种巨大的活动swoosh 自定义适配器的getView方法Android 具有HttpResponseCache的Gridview性能,android,android-gridview,Android,Android Gridview,我使用gridview显示数百张图像(甚至可能几千张)。图像位于服务器上,我正在使用缓存图像。我遇到的问题是,当我在gridview中向下滑动时,在最终确定正确的图像之前,每个子视图的回收视图显示3个或更多图像。这似乎是回调方法返回所有请求的图像的结果。如何使gridview在上/下滚动时不出现这种巨大的活动swoosh 自定义适配器的getView方法 public View getView(int position, View convertView, ViewGroup parent) {
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_216;.GetImageAsync调用自定义异步任务
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;
}
}
公共类AsyncRequestImage扩展了AsyncTask{
HttpURLConnection;
流内输入流;
IApiCallback回调函数;
布尔ignoreCache;
公共AsyncRequestImage(IApiCallback回调,布尔ignoreCache){
this.callback=回调;
this.ignoreCache u=ignoreCache;
}
@凌驾
受保护对象doInBackground(字符串…uri){
位图图像;
如果(忽略缓存){
image=acquireImage(uri[0],true);
}否则{
image=acquireImage(uri[0],false);
if(image==null)
image=acquireImage(uri[0],true);
}
返回图像;
}
@凌驾
受保护的void onPostExecute(对象映像){
回调执行(图像);
}
私有位图acquireImage(字符串url、布尔ignoreCache){
试一试{
URL URL=新URL(URL);
连接=(HttpURLConnection)\u url.openConnection();
连接\ addRequestProperty(“接受编码”、“gzip”);
if(ignoreCache){
连接.setRequestProperty(“缓存控制”,“最大年龄=0”);
}否则{
连接\ addRequestProperty(“缓存控制”,“仅当缓存时”);
}
连接uu.connect();
String encoding=connection.getContentEncoding();
//确定流是否已压缩,如果需要,将其解压缩。
if(encoding!=null&&encoding.equalsIgnoreCase(“gzip”)){
试一试{
inStream_u3;=新的GZIPInputStream(connection_3;.getInputStream());
}catch(filenotfounde异常){
}
}否则{
试一试{
inStream_ux=连接_ux.getInputStream();
}catch(filenotfounde异常){
}
}
如果(流内_932;!=null){
试一试{
位图图像=位图工厂.decodeStream(流内);
返回图像;
}catch(java.lang.OutOfMemoryError oom){
FileLogger.getFileLogger().ReportInfo(“UrlConnection:位图创建失败。内存不足”);
}
}
}捕获(IOE异常){
如果(e!=null&&e.getMessage()!=null){
i(“AsyncRequestImage doInBackground:,e.getMessage());
}
}最后{
连接断开();
}
返回null;
}
}
我遇到的部分问题是由于BaseAdapter.GetView未优化
另外,当用户发起一个投掷动作时,我仍然试图在视图经过时加载所有图像
!!为我所犯的每一个错误提供了详细的描述和解决方案。这篇文章中还有一个到源代码的链接,该链接提供了一种方法,可以在投掷动作完成之前停止加载图像。我遇到的部分问题是由于BaseAdapter.GetView未优化 另外,当用户发起一个投掷动作时,我仍然试图在视图经过时加载所有图像 !!为我所犯的每一个错误提供了详细的描述和解决方案。在那篇文章中还有一个到源代码的链接,该链接提供了一种在投掷动作完成之前停止加载图像的方法