1

我正在使用 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;
}

}
4

1 回答 1

1

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

此外,当用户启动一个投掷手势时,我仍在尝试在视图经过时加载所有图像。

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

于 2013-01-06T08:18:05.660 回答