首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用HttpResponseCache的Gridview性能

使用HttpResponseCache的Gridview性能
EN

Stack Overflow用户
提问于 2013-01-06 06:45:17
回答 1查看 402关注 0票数 1

我正在使用网格视图来显示数百张图片(甚至可能是几千张)。图像位于服务器上,我使用HttpResponseCache缓存图像。我遇到的问题是,当我在网格视图中向下滑动时,回收的视图在最终确定正确的图像之前,每个子视图显示3个或更多图像。这似乎是回调方法返回所有请求图像的结果。当向上/向下滚动时,如何才能使网格视图不会出现这种巨大的活动。

我的自定义适配器的getView方法

代码语言:javascript
复制
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;           
}

设置图像的回调方法。

代码语言:javascript
复制
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

代码语言:javascript
复制
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;
}

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-06 16:18:06

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

此外,当用户发起一个fling手势时,我仍然试图在视图经过时加载所有图像。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14177227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档