如何避免动画GIF的内存泄漏

时间:2018-02-11 11:25:52

标签: java android animation memory-leaks

我从服务器接收应该转换为GIF的URL,这些GIF应该在用户点击它们或者从任何用户收到响应时动画,一切正常但是当我收到大量的GIF时内存泄漏,它不会内存但是内存不断增加并且一直面临着hickups,请告知可以做什么以避免将它们放在内存中,我已经尝试用null初始化animImageView但是仍然同样的态度,请告知可以做些什么。

public void getGifts() {
    Communicator.getInstance().on("sendGift", new Emitter.Listener() {

        Animation anim;
        EmojiModel emojiUrl = new EmojiModel();
        String url = emojiUrl.getUrlFile();

        @Override
        public void call(Object... args) {
            JSONDictionary response = (JSONDictionary) args[0];
            url = (String) response.get("giftUrl");

            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    animImageView = null;
                    animImageView = new ImageView(getApplicationContext());
                    animImageView.setVisibility(View.VISIBLE);

                    Glide.with(getApplicationContext())
                            .load(url)
                            .thumbnail(0.1f)
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .crossFade()
                            .override(900,400)
                            .into(animImageView);


                    final RelativeLayout relativeLayout = 
                    (RelativeLayout)findViewById(R.id.imageView_relative);
                    anim = 
            AnimationUtils.loadAnimation(getApplicationContext(), 
            R.anim.move_to_right_anim);
                    relativeLayout.addView(animImageView);
                    animImageView.startAnimation(anim);

                    StreamActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            anim.setAnimationListener(new 
                            Animation.AnimationListener() {

                        @Override
                        public void onAnimationStart(Animation animation) {
              anim =AnimationUtils.loadAnimation(getApplicationContext(), 
              R.anim.move_to_right_anim);
              animImageView.startAnimation(anim);
             }

             @Override
             public void onAnimationEnd(Animation animation) {
                animImageView = null;
             }

             @Override
             public void onAnimationRepeat(Animation animation) {
                animImageView = null;
             }
           });
          }
        });
       }
      };
        mhandler.post(runnable);
     }
  });
}

1 个答案:

答案 0 :(得分:0)

这已通过添加relativeLayout.removeAllViewsInLayout()解决;在添加视图之前