Android利用ViewPager实现图片浏览,解决内存问题

在Android开发中,我们经常遇到利用Viewpager实现图片浏览的需求,这个很好实现,也没什么技术难点,但是最近我在实现这个功能的时候,遇到了一些问题,当图片数量稍微多点时,一直滑动浏览,会导致内存占用一路走高,最终导致oom,在分析了只有,发现是因为viewpager中给的ImageIView一直在新建导致的内存溢出,最后找到了解决方法,让view实现复用,解决了内存问题,代码也比较简单,复写viewdaptaer的instantiateItem()方法,代码如下

public class CarImagesBrowseAdapter extends PagerAdapter {

    private ArrayList mDatas;//数据源
    private LinkedList mViewCache;//缓存view
    private Context mContext;
    private int mChildCount;

    public CarImagesBrowseAdapter(Context context, ArrayList arrayList) {
        this.mContext = context;
        this.mDatas = arrayList;
        mViewCache = new LinkedList<>();
    }

    @Override
    public void notifyDataSetChanged() {
        mChildCount = getCount();
        super.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return this.mDatas.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    @Override
    public int getItemPosition(Object object) {
        if (mChildCount > 0) {
            mChildCount--;
            return POSITION_NONE;
        }
        return super.getItemPosition(object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View convertView = null;
        if (mViewCache.size() == 0) {
            PhotoDraweeView draweeView = new PhotoDraweeView(mContext);
            draweeView.setBackgroundColor(0xff000000);
            convertView = draweeView;
        } else {
            convertView = mViewCache.removeFirst();
        }
         //这里我是用Facebook的Fresco加载的图片,你可以在这里换成你使用的图片加载方式
        final PhotoDraweeView view = (PhotoDraweeView) convertView;
        PipelineDraweeControllerBuilder controller = Fresco.newDraweeControllerBuilder();
        controller.setUri(Uri.parse(Constants.PICTURE_HOST + mDatas.get(position)));
        controller.setOldController(view.getController());
        controller.setControllerListener(new BaseControllerListener() {
            @Override
            public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                super.onFinalImageSet(id, imageInfo, animatable);
                if (imageInfo == null) {
                    return;
                }
                view.update(imageInfo.getWidth(), imageInfo.getHeight());
            }
        });
        view.setController(controller.build());
        container.addView(convertView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        return convertView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View contentView = (View) object;
        container.removeView(contentView);
        this.mViewCache.add(contentView);
    }
}

代码也比较简单,相信大家也都能看懂!

你可能感兴趣的:(Android利用ViewPager实现图片浏览,解决内存问题)