在ViewPager

时间:2015-05-25 11:42:46

标签: android android-fragments android-viewpager out-of-memory

由于Bitmaps,我出现了内存问题。我的应用程序是一个水平的图像轮播,使用ViewPager和片段实现。

那么有人可以指出如何回收片段以及在哪里做到这一点吗?

以下是我的代码

public ImageSliderFragment(){}

public void initializeEvent(OnExpeditionImageViewTouch event){
    this.event = event;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(
            R.layout.view_pager_single_frame_layout, container, false);
    Bundle args = getArguments();
    final int pos = args.getInt(AppConstants.BUNDLE_KEY_FOR_FRAGMENT_POSITION);
    position = pos;
    expedition_image = ((ImageView) rootView.findViewById(R.id.stackView));
    try {
        expedition_image.setImageBitmap(decodeSampledBitmapFromResource(getResources(), AppConstants.imageResource.get(position), 400, 400));

    } catch (Exception e) {
    }


    return rootView;
}

@Override
public void setExitSharedElementCallback(SharedElementCallback callback) {
    super.setExitSharedElementCallback(callback);
}

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                     int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }
    return inSampleSize;
}

PagerAdapter代码

 private class CustomViewPagerAdapter extends FragmentStatePagerAdapter {
    private Map<Integer, ImageSliderFragment> mPageReferenceMap = new HashMap<>();

    public CustomViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        ImageSliderFragment imageSliderFragment = new ImageSliderFragment();
        Bundle args = new Bundle();
        args.putInt(AppConstants.BUNDLE_KEY_FOR_FRAGMENT_POSITION,position);
        imageSliderFragment.setArguments(args);
        mPageReferenceMap.put(Integer.valueOf(position),imageSliderFragment);
        return imageSliderFragment;
    }

    @Override
    public int getCount() {
        return AppConstants.NUM_PAGES;
    }

    @Override
    public int getItemPosition(Object object) {
        return /**/POSITION_NONE;//super.getItemPosition(object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        return  super.instantiateItem(container, position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //ImageSliderFragment fragment = (ImageSliderFragment)object;
        super.destroyItem(container, position, object);
    }

    public ImageSliderFragment getFragment(int key) {
        return mPageReferenceMap.get(key);
    }
}

3 个答案:

答案 0 :(得分:1)

在清单中尝试此操作,它将为viewpager

分配更多内存
android:largeHeap="true"

答案 1 :(得分:0)

您可以将ImageView设置为此链接中提到的WeakReference:
http://developer.android.com/training/displaying-bitmaps/process-bitmap.html

imageViewReference = new WeakReference<ImageView>(imageView);

答案 2 :(得分:-3)

我已经通过细微修改解决了这个问题,

以下是修改后的代码

在ImageSliderFragment类

public static ImageSliderFragment newInstance(int imageNum) {
    final ImageSliderFragment f = new ImageSliderFragment();
    final Bundle args = new Bundle();
    args.putInt(AppConstants.BUNDLE_KEY_FOR_FRAGMENT_POSITION, imageNum);
    f.setArguments(args);
    return f;
}

我的CustomViewPagerAdapter getItem如下所示;

@Override
    public Fragment getItem(int position) {
        return ImageSliderFragment.newInstance(position);
    }