延迟加载图片listView

时间:2013-10-14 23:20:43

标签: java android universal-image-loader

我有一个列表视图,每个单元格显示一个大图像。我正在使用通用图像加载器安卓库将图像加载到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,cacheInDisk设置为true。

1 个答案:

答案 0 :(得分:0)

也许有点晚了。 但是尝试从getView()中删除imageLoader.init()调用。您必须在整个应用程序中只调用一次init。