- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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/
我关注了 developers guide并且我通过在应用程序的 onCreate() 方法中包含以下代码来安装 HttpResponseCache: try { File httpCacheDi
我很快将在一个项目上工作,该项目使用大量 HTTPRequests 主要用于 JSON 和 Images,所以我认为它是考虑缓存是个好主意。基本上我正在寻找 的解决方案 启动具有给定生命周期(例如 3
我正在尝试使用 HttpResponseCache,但没能成功。 我是这样安装的: SdCardManager manager = SdCardManager.getSdCardMan
我想使用 HttpResponseCache 我的场景如下: 当互联网连接可用时,服务器上的数据会正常检索并保存到缓存中。 当没有互联网连接时,将检索并显示缓存数据。 我在以下 GitHub API
我正在尝试缓存此文件:http://media.admob.com/sdk-core-v40.js与 HttpResponseCache。基本上我调用了 getFileFromUrl 函数两次,以便第
我在 Android 中缓存来自 Web 服务器的 http(s) 响应时遇到问题,文档很差,所以我在这里寻求帮助。这是我的代码: String encodedString = String.
我正在尝试使用 HttpResponseCache 来缓存网络数据,但是当我将安装缓存代码放入 OnCreate 和 OnPause 方法时,应用崩溃。我使用了 HttpResponseCache d
我正在使用 gridview 显示数百张图像(甚至可能是几千张)。图片位于服务器上,我正在使用 HttpResponseCache 缓存图片.我遇到的问题是,当我向下滑动 gridview 时,回收的
我正在开发一个与 API 14( Ice Cream Sandwich )兼容的 Android 应用程序。我找到了可用于缓存 HTTP 请求的类 HttpResponseCache。我想将缓存与我的
我正在使用 HttpResponseCache在我的 Android 应用程序中启用响应缓存(用于 Web 请求),并且离线缓存不工作。我正在做离线缓存作为 documentation告诉我去做。 在
我需要缓存 http 响应,并基于此 blog post该功能内置于 ICS 中。但是,我需要支持 1.6 版及更高版本,因此想在查看向后移植 ICS 功能需要什么之前先看看是否有可用的库。 更新:
我正在尝试在导入的Android Studio中编译一个Android项目: com.squareup.okhttp.HttpResponseCache 我收到错误消息“无法解析符号'HttpResp
我一直在我的应用程序中成功使用 HttpResponseCache,但是当我的手机更新到 Lollipop 时,我意识到 HttpResponseCache 现在永远不会“命中”,总是执行网络请求。我
您好,我正在尝试使用 Android 4 中引入的 HttpResponseCache。文档确实清楚地讨论了如何安装缓存,但我完全不知道如何缓存从网络下载的图像。之前我使用的是 DiskLruCach
我正在尝试使用 httpResponseCache缓存我的一些请求。问题是,我想知道在发出请求之前是否缓存了请求。 我在源代码中看到了 HttpResponseCache有一个 get 方法接受 (U
最初的目标是同时使用磁盘和内存缓存。这需要实现 LRU 缓存和 DiskLruCache。但是,由于 HTTPResponse 缓存使用磁盘空间,我选择使用 LRU 缓存并执行 con.setUseC
我正在尝试在一个新的 Android 项目中测试这个缓存库。 当我运行项目时,我得到这个 Logcat: 01-24 03:45:31.109: E/AndroidRuntime(1983): FAT
我有以下方法从 wiktionary.org 请求页面,问题是服务器在 header 中返回 Cache-control => private, must-revalidate, max-age=0
我正在开发一个基于 WebView 的应用程序,该应用程序当前在 v3.1 平板电脑上运行。我似乎无法让 WebView 缓存 css、js 和图像(或使用缓存)。该应用似乎总是连接到服务器,服务器返
我正在尝试在我的应用程序中使用内置的 HTTPResponseCache(通过 HTTPURLConnection API 发出请求)但是在尝试让它缓存任何响应时遇到了问题请求时包含一个 Author
我是一名优秀的程序员,十分优秀!