【问题标题】:Keep the ImageView's Final View while scrolling滚动时保持 ImageView 的最终视图
【发布时间】:2013-09-05 05:01:41
【问题描述】:

我正在使用 Universal-Image-Loader 来处理我的图像的显示,我正在使用类似 gridview 的实现来显示图像。在显示图像之前,它将首先下载并缓存到 sd 卡中。在图像仍在下载的同时,我使用每个图像的惰性列表实现来处理视图。

问题:

下载完所有图片并准备好展示后, 每次我滚动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();

我试过了,设置还是一样。

【问题讨论】:

  • 你能发布你的UIL配置的完整代码吗?
  • 试试看更新先生。
  • 使用 androidquery 框架非常棒,它负责缓存管理和异步加载图像。在这里找到code.google.com/p/android-query
  • @BirajZalavadia 不确定这是否可以解决我在 scolling 时保留图像的问题,androidquery 我已经习惯了 UIL。
  • 如果我理解正确,问题在于重新创建 gridView 的元素(GridViewListView 不包含所有元素,但在它们变得可见时创建它们)。你可以编写你的适配器来保留所有元素并返回一个现有的而不是创建它)。

标签: java android caching universal-image-loader


【解决方案1】:

您应该做的是,您需要将图像缓存在某个目录中,例如我们的应用程序缓存。图像所在的位置而不是内存缓存。使用文件缓存不会增加堆大小,下载后会立即从目录中获取图像。

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();

这将在开始下载到目录之前清除磁盘。当您的目录有旧图像时,这很有用。

内存缓存与磁盘缓存

堆是指您的应用程序内存分配。如果您不提供磁盘缓存,假设您的图像的固定大小为 100KB,则 10 个大小为 1 MB 的图像将存储在内存中而不是磁盘中。虽然磁盘不是内存。它是一个目录,物理上位于内部磁盘上,内部有自己的空间。因此,磁盘缓存不是提供内存缓存,而是根据内存利用率很好。是的,它确实有点慢,但这永远不会影响你的表现。

【讨论】:

  • 我试着比较了性能,它确实很顺利。但不确定这意味着什么"not increase Heap size and images will fetch from directory immediately after download",系统在第一个配置上做了什么?另外,我不确定这些代码的用途是什么,.clearMemoryCache().clearDiscCache()。另外,我试图找到缓存目录,但那里什么也没有。
  • data/yourapp/cache/missing folder_directory_name
  • 将根据我的代码创建目录。我已经尝试过我的一个项目并且运行良好。
  • 是的,确实如此。感谢您提供帮助!我只是从你那里学来的。!
  • Chintan 先生,我所做的是将其更改为 getExternalCacheDir() 而不是 getCacheDir()。现在我可以在外部看到它了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 2010-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-17
相关资源
最近更新 更多