Recycle ImageViews to avoid OutOfMemoryError

2019-08-13 19:18发布

In some phones (especially Samsungs), the application gives an error of OutOfMemory.

I use a ViewFlipper with 4 ImageViews which they are shown in a tutorial.

I have found no way to remedy the error or recycle imageviews.

This is the method that manages the ViewFlipper:

private void changeBackground(int position) {

    switch (position) {

        case 1:
            mViewFlipper.setDisplayedChild(1);
            mViewFlipper.setInAnimation(getActivity(), R.anim.anim_clockwise_onboarding);
            mViewFlipper.setOutAnimation(getActivity(), R.anim.anim_clockwise_inverse_onboarding);
            break;

        case 2:
            mViewFlipper.setDisplayedChild(2);
            mViewFlipper.setInAnimation(getActivity(), R.anim.anim_clockwise_onboarding);
            mViewFlipper.setOutAnimation(getActivity(), R.anim.anim_clockwise_inverse_onboarding);
            break;

        case 3:
            mViewFlipper.setDisplayedChild(3);
            mViewFlipper.setInAnimation(getActivity(), R.anim.anim_clockwise_onboarding);
            mViewFlipper.setOutAnimation(getActivity(), R.anim.anim_clockwise_inverse_onboarding);
            break;

        default:
            mViewFlipper.setDisplayedChild(0);
            mViewFlipper.setInAnimation(getActivity(), R.anim.anim_clockwise_onboarding);
            mViewFlipper.setOutAnimation(getActivity(), R.anim.anim_clockwise_inverse_onboarding);
            break;

    }

}

The method is called each time you change pages in a ViewPager.

@Override
public void onPageSelected(int position) {
       changeBackground(mViewPager.getCurrentItem());
}

If any have idea please help me, Thanks in advance.

1条回答
Root(大扎)
2楼-- · 2019-08-13 20:00

For that you need to use Image loader library for loading images. You can use :

  1. Universal Image loader
  2. Picasso
  3. Glide

This library uses for bitmap memory management and after using this library you didn't face OutOfMemoryError exeption.

查看更多
登录 后发表回答