我正在使用 Universal-Image-Loader 来处理我的图像显示,我正在使用类似 gridview 的实现来显示图像。在显示图像之前,它将首先下载并缓存到 SD 卡中。在图像仍在下载的同时,我为每个图像使用惰性列表实现来处理 View 。
问题:
在所有图像下载并准备好显示之后 每次我滚动 gridview,向上和向下方向图像有点被惰性列表再次下载, 我相信它是从我的应用程序的缓存文件夹(sd 卡)下载的。有什么办法可以让我在滚动时保持图像(最终状态)?这样我就不需要每次滚动时都下载它来实现平滑滚动。?我需要等到图像从 SD 卡中完全加载,以便我可以平滑滚动。
我从 UIL 获得了这个设置,但我没有看到任何影响。
.resetViewBeforeLoading(false|true)
编辑
我也试过,
imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(getActivity()));
同下
HttpParams params = new BasicHttpParams();
// Turn off stale checking. Our connections break all the time anyway,
// and it's not worth it to pay the penalty of checking every time.
HttpConnectionParams.setStaleCheckingEnabled(params, false);
// Default connection and socket timeout of 10 seconds. Tweak to taste.
HttpConnectionParams.setConnectionTimeout(params, 10 * 1000);
HttpConnectionParams.setSoTimeout(params, 10 * 1000);
HttpConnectionParams.setSocketBufferSize(params, 8192);
ImageLoaderConfiguration config =
new ImageLoaderConfiguration
.Builder(getActivity())
.threadPoolSize(3)
.memoryCache(new WeakMemoryCache())
.imageDownloader(new HttpClientImageDownloader(getActivity(), new DefaultHttpClient(manager, params)))
.build();
imageLoader.init(config);
options = new DisplayImageOptions.Builder()
.showStubImage(icon)
.resetViewBeforeLoading(false)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisc(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
我试过试验,设置仍然相同。
最佳答案
您应该做的是,您需要将图像缓存在某个目录中,例如我们的应用程序缓存。图像所在的位置而不是内存缓存。使用文件缓存不会增加堆大小,图像会在下载后立即从目录中获取。
File cacheDir = new File(this.getCacheDir(), "directory_name");
if (!cacheDir.exists())
cacheDir.mkdir();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
YourActivity.this).threadPoolSize(5)
.threadPriority(Thread.MIN_PRIORITY + 3)
.denyCacheImageMultipleSizesInMemory()
// .memoryCache(new UsingFreqLimitedMemoryCache(2000000)) // You
// can pass your own memory cache implementation
.memoryCacheSize(1048576 * 10)
// 1MB=1048576 *declare 20 or more size if images are more than
// 200
.discCache(new UnlimitedDiscCache(cacheDir))
// You can pass your own disc cache implementation
// .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
imageLoader.clearMemoryCache();
imageLoader.clearDiscCache();
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.avatar)
.showImageForEmptyUri(R.drawable.home_shortcut).cacheInMemory()
.cacheOnDisc().build();
注意:我使用的是旧版本的 UIL。
目的
imageLoader.clearMemoryCache();
这将在您再次运行此代码时清除内存。当您的 URL 相同但图像不同时,这很有用。
imageLoader.clearDiscCache();
这将在开始下载到目录之前清除磁盘。当您的目录有旧图像时,这很有用。
内存缓存与磁盘缓存
Heap 表示您的应用程序内存分配。如果您不提供磁盘缓存,假设您的图像固定大小为 100KB,则 10 张大小为 1MB 的图像将存储在内存中而不是磁盘中。虽然磁盘不是内存。它是一个目录,物理上位于内部磁盘上,内部有自己的空间。因此,磁盘缓存不是提供内存缓存,而是根据内存利用率提供良好的服务。是的,它确实有点慢,但这绝不会影响您的表现。
关于java - 滚动时保持 ImageView 的最终 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18567234/