我正在尝试将 Universal Image Loader 集成到我的 Android 应用程序中。它有一个 GridView 并显示从互联网获取的图像。我使用 ArrayAdapter 实现了它,它以通常的方式在 getView() 中加载图像。
它在正确显示图片方面效果很好。但是我发现从内存缓存加载图像时出现了意外行为。
- 启动活动时,UIL 从 Internet 或磁盘缓存(如果存在)加载图像。(当然,这是预期的行为。)
 - 向下滚动 GridView 直到第一列离开屏幕,然后滚动回顶部。此时,第一列的图像是从磁盘缓存加载的,而不是从内存缓存加载的。
 - 然后再次向下和向上滚动。此时,第一列的图像从内存缓存中加载。
 
我希望图像在第二次显示时从内存缓存中加载,这是上面操作中的第 2 步。我不知道为什么在这种情况下使用磁盘缓存。
这是我的代码。
图像加载器配置
ImageLoaderConfiguration mImageLoaderConfig =
        new ImageLoaderConfiguration.Builder(getApplicationContext())
                .defaultDisplayImageOptions(defaultOptions)
                .enableLogging()
                .build();
显示图像选项
DisplayImageOptions defaultOptions =
        new DisplayImageOptions.Builder()
                .cacheInMemory()
                .cacheOnDisc()
                .showImageForEmptyUri(R.drawable.empty_photo)
                .showStubImage(R.drawable.empty_photo)
                .displayer(new FadeInBitmapDisplayer(500))
                .build();
ArrayAdapter 中的 getView()
if (convertView == null) {
    convertView = (FrameLayout) LayoutInflater.from(getContext())
            .inflate(mLayoutId, null);
    convertView.setLayoutParams(mImageViewLayoutParams);
} else { // Otherwise re-use the converted view
    convertView.findViewById(R.id.videoIconInThumbnail).setVisibility(View.GONE);
}
// Check the height matches our calculated column width
if (convertView.getLayoutParams().height != mItemHeight) {
    convertView.setLayoutParams(mImageViewLayoutParams);
}
ImageView image = (ImageView) convertView.findViewById(R.id.photoThumbnail);
ImageLoader.getInstance().displayImage(thumbnailUrl, image,
        new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                Log.v(TAG, imageUri + " is loaded.");
            }
        });
return convertView;
GridView 中元素的布局 XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ImageView
        android:id="@+id/photoThumbnail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" >
    </ImageView>
    <ImageView
        android:id="@+id/videoIconInThumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/ic_play"
        android:visibility="gone" >
    </ImageView>
</FrameLayout>
UIL 的版本是 1.8.4。测试的安卓版本是4.1.2。
添加了使用上述操作加载图像 3 次时的 UIL 日志输出。
// Fist time of displaying
I/ImageLoader( 7404): Start display image task [http://xxx/yyy.JPG_1080x1776]
I/ImageLoader( 7404): Load image from disc cache [http://xxx/yyy.JPG_1080x1776]
I/ImageLoader( 7404): Subsample original image (x192) to x192 (scale = 1) [http://xxx/yyy.JPG_1080x1776]
I/ImageLoader( 7404): Cache image in memory [http://xxx/yyy.JPG_1080x1776]
I/ImageLoader( 7404): Display image in ImageView [http://xxx/yyy.JPG_1080x1776]
// Second time of displaying
I/ImageLoader( 7404): ImageLoader is paused. Waiting...  [http://xxx/yyy.JPG_358x357]
I/ImageLoader( 7404): Start display image task [http://xxx/yyy.JPG_358x357]
I/ImageLoader( 7404): Load image from disc cache [http://xxx/yyy.JPG_358x357]
I/ImageLoader( 7404): Subsample original image (x192) to x192 (scale = 1) [http://xxx/yyy.JPG_358x357]
I/ImageLoader( 7404): Cache image in memory [http://xxx/yyy.JPG_358x357]
I/ImageLoader( 7404): Display image in ImageView [http://xxx/yyy.JPG_358x357]
// Third time of displaying
I/ImageLoader( 7404): Load image from memory cache [http://xxx/yyy.JPG_358x357]
谢谢你。