Recycle ImageViews to avoid OutOfMemoryError

喜欢而已 提交于 2019-12-24 06:00:31

问题


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:


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.



来源:https://stackoverflow.com/questions/36354229/recycle-imageviews-to-avoid-outofmemoryerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!