简体   繁体   English

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

[英]how to avoid memory leakage from an animated GIF

I'm receiving URLs from server which should be translated to GIFs, These GIFs should animate whenever the user clicks on them or when receiving a response from any of the users, everything is working fine however when receiving lots of them I experience memory leakage, it doesn't go out of memory however the memory keeps increasing constantly and keeps facing hickups, please advise what can be done in order to avoid having them on the memory, I've tried initializing the animImageView with null however still the same attitude, please advise what can be done. 我从服务器接收到应该转换为GIF的URL,这些GIF应当在用户单击时或在收到任何用户的响应时都具有动画效果,但是一切正常,但是在接收很多URL时,我会遇到内存泄漏,它不会耗尽内存,但是内存一直在不断增加,并且不断遇到问题,请告知可以做些什么以避免它们出现在内存中,我尝试使用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);
     }
  });
}

This has been resolved by adding relativeLayout.removeAllViewsInLayout(); 通过添加relativeLayout.removeAllViewsInLayout()可以解决此问题。 before adding the view 在添加视图之前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM