我有一个列表视图,每个单元格都显示一个大图像。我正在使用通用图像加载器 android 库将图像加载到 ImageViews 中。我将图像存储设置为 chace 和磁盘 true,但列表滚动不顺畅(我怀疑缓存是否正常工作)。所以,我创建了一个位图数组来存储加载的图像,但现在我遇到了“内存不足错误”的问题。
这是代码:
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
socialHolder holder = null;
Object obj = arrayList.get(position);
Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/big_noodle_titling.ttf");
row = mInflater.inflate(R.layout.social_cell, null);
holder = new socialHolder();
holder.image = (ImageView)row.findViewById(R.id.socialCellImage);
holder.title = (TextView)row.findViewById(R.id.socialCellTitle);
holder.location = (TextView)row.findViewById(R.id.socialCellLocation);
holder.date = (TextView)row.findViewById(R.id.socialCellDate);
final Event ev = (Event)obj;
socialCell social = new socialCell(ev.imageURL,ev.title,ev.date,ev.location.name);
com.nostra13.universalimageloader.core.ImageLoader imageLoader = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
holder.title.setText(social.title);
if (!contains(loaded,position)) {
DisplayImageOptions conf = new DisplayImageOptions.Builder().cacheInMemory(false).cacheOnDisc(false).build();
if (social.image != null) imageLoader.displayImage(social.image, holder.image, conf, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
@Override
public void onLoadingFailed(String imageUri, View view,FailReason failReason) {
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
loadedImage.compress(Bitmap.CompressFormat.JPEG, 100, stream);
ev.realImage = stream.toByteArray();
loadedImages[position] = loadedImage;
loaded[position] = position;
}
});
else {
holder.image.setImageBitmap(loadedImages[position]);
}
PS:我已经将 cacheInMemory 和 cacheInDisk 设置为 true。