我的应用程序有带图像的 ListView。它使用 Universal Image Loader (1.7.0) 从 Internet 加载图像。
配置和显示选项:
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory()
.cacheOnDisc()
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
.defaultDisplayImageOptions(defaultOptions)
.threadPoolSize(2)
.enableLogging()
.build();
getView() 的一部分(我的应用重用 ConvertView 并使用 ViewHolder 模式):
View rowView = convertView;
ViewHolder holder;
if (rowView == null) {
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(R.layout.list_row, null, true);
holder = new ViewHolder();
holder.photoView = (ImageView) rowView.findViewById(R.id.list_photo_view);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
String photoUrl = "http://someserver.com/some_image.jpg";
mImageLoader.displayImage(photoUrl, holder.photoView);
list_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:clickable="true" >
<ImageView
android:id="@+id/list_photo_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="false"
android:contentDescription="@string/photo"
android:scaleType="centerInside"
android:src="@drawable/template_image_list" />
</LinearLayout>
当然 ImageView 不是每一行中的单个视图。这是简短的版本:)。所以我从互联网上下载的图像尺寸是 450x337。我正在平板电脑上测试我的应用程序,屏幕尺寸为 1280x760。
当我启用日志记录时 - 我看到这样的消息:
Load image from memory cache [http://someserver.com/some_image.jpg_1280x736]
等等......我对 url 结尾有点困惑 - “_1280x736”。正如手册的第二部分所说:
• maxImageWidthForMemoryCache() 和 maxImageHeightForMemoryCache() 用于将图像解码为位图对象。为了不在内存中存储全尺寸图像,将其缩小到由 ImageView 参数值确定的大小,其中加载图像:maxWidth 和 maxHeight(第一阶段),layout_width 和 layout_height(第二阶段)。如果未定义这些参数(值 fill_parent 和 wrap_content 被认为是不确定的),则采用设置 maxImageWidthForMemoryCache() 和 maxImageHeightForMemoryCache() 指定的尺寸。原图尺寸缩小2倍(推荐快速解码),直到宽度或高度小于指定值;o 默认值 - 设备屏幕的大小。
所以,据我所知,这个结尾是“减少”之后内存缓存中图像的大小:) 并且图像加载器使用了默认值(屏幕大小)。我并不惊讶图像加载器无法获得 ImageView 的大小(可能为时过早),但为什么原始图像被放大了?是否很难验证原始图像的大小并且不加修改地保留它们?当然,我可以在 displayImage 方法中添加图像大小,但我需要解释......