[英]Lazy loading image listView
我有一個listview,每個單元格顯示一個大圖像。 我正在使用通用圖像加載器android庫將圖像加載到ImageViews中。 我在chace和disk上設置了圖像存儲為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和true設置為true。
也許有點晚。 但是,請嘗試從getView()中刪除imageLoader.init()調用。 您只需在整個應用程序中調用一次init。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.