how to recycle images as frame of AnimationDrawable and solve OutOfMemory error in android?
Asked Answered
V

0

0

I want to show some exercises in my android app with ViewPager. i use:

  • 2 imageView ,one of them shows exercises and others shows th counter
  • AnimationDrawable

    as i eun my app,it has been crashed and shows me OutOFMemory error.although android shows all of images automatically, how can i recycle images after showing?

    public class NeckSportFinger extends Fragment {
        private ImageView sportShowing_img, timeShowing_img;
        public static AnimationDrawable animation_sport, animation_time;
        public TextView sportText;
    
        public NeckSportFinger(){}
    
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_sport_show, container, false);
    
            sportShowing_img= (ImageView) view.findViewById(R.id.sport_img);
            timeShowing_img= (ImageView) view.findViewById(R.id.time_img);
    
            animation_sport = new AnimationDrawable();
            animation_time = new AnimationDrawable();
    
            animation_sport.addFrame(getResources().getDrawable(R.drawable.neck_one), 1500); //1 Sec
            animation_sport.addFrame(getResources().getDrawable(R.drawable.neck_one_right), 8500); //1 Sec
            animation_sport.addFrame(getResources().getDrawable(R.drawable.neck_one), 1500); //1 Sec
            animation_sport.addFrame(getResources().getDrawable(R.drawable.neck_one_left), 8500); //1 Sec
    
    
    
    
            animation_time.addFrame(getResources().getDrawable(R.drawable.t0), 1500); //1 Sec
            animation_time.addFrame(getResources().getDrawable(R.drawable.t1), 1500); //1 Sec
            animation_time.addFrame(getResources().getDrawable(R.drawable.t2), 1000); //1 Sec
            animation_time.addFrame(getResources().getDrawable(R.drawable.t3), 1000); //1 Sec
            animation_time.addFrame(getResources().getDrawable(R.drawable.t4), 1000); //1 Sec
            animation_time.addFrame(getResources().getDrawable(R.drawable.t5), 1000); //1 Sec
            animation_time.addFrame(getResources().getDrawable(R.drawable.t6), 1000); //1 Sec
            animation_time.addFrame(getResources().getDrawable(R.drawable.t7), 1000); //1 Sec
            animation_time.addFrame(getResources().getDrawable(R.drawable.t8), 1000); //1 Sec
    
            sportShowing_img.setBackgroundDrawable(animation_sport);
            timeShowing_img.setBackgroundDrawable(animation_time);
    
    
            sportText= (TextView) view.findViewById(R.id.sport_type);
            sportText.setText(R.string.sport_finger);
    
            return view;
        }
    
    
    
        public void start() {
            animation_sport.start();
            animation_time.start();
        }
    
        public void stop() {
            animation_sport.stop();
            animation_time.stop();
        }
    }
    

thanks!

Valina answered 16/8, 2017 at 9:12 Comment(2)
is it crashing while you are displaying the fragment with the animations or later?Humane
i've 7 exercise group that showing in ViewPager. as i scroll them, it crash in fourth.Valina

© 2022 - 2024 — McMap. All rights reserved.