zzz40500/android-shapeLoadingView

fix memory leak

Closed this issue · 2 comments

Can't stop shapeLoadingView, there are some memory leak in widget.
This is a patch to fix this bug.

Subject: [PATCH] fix memory leak

Change-Id: I704c3f34bebc586fb4ceeee758115625a7a5776f

---

diff 

         addView(view, layoutParams);

+        startLoading(900);
     }

     public void setLoadingText(CharSequence loadingText) {
         loadTextView.setText(loadingText);
+    }
+
+    private AnimatorSet mAnimatorSet = null;
+
+    private Runnable mFreeFallRunnable = new Runnable() {
+        @Override
+        public void run() {
+            freeFall();
+        }
+    };
+
+    private void startLoading(long delay) {
+        if (mAnimatorSet != null && mAnimatorSet.isRunning()) {
+            return;
+        }
+        this.removeCallbacks(mFreeFallRunnable);
+        if (delay > 0) {
+        this.postDelayed(mFreeFallRunnable, delay);
+    } else {
+        this.post(mFreeFallRunnable);
+        }
+    }
+
+    private void stopLoading() {
+        if (mAnimatorSet != null) {
+            if (mAnimatorSet.isRunning()) {
+                mAnimatorSet.cancel();
+            }
+            mAnimatorSet = null;
+        }
+        this.removeCallbacks(mFreeFallRunnable);
+    }
+
+    @Override
+    public void setVisibility(int visibility) {
+        super.setVisibility(visibility);
+        if (visibility == View.VISIBLE) {
+            startLoading();
+        } else {
+            stopLoading();
+        }
     }

     /**
@@ -113,18 +143,13 @@
         ObjectAnimator objectAnimator1 = null;
         switch (shapeLoadingView.getShape()) {
             case SHAPE_RECT:
-
                 objectAnimator1 = ObjectAnimator.ofFloat(shapeLoadingView, "rotation", 0, -120);
-
                 break;
             case SHAPE_CIRCLE:
                 objectAnimator1 = ObjectAnimator.ofFloat(shapeLoadingView, "rotation", 0, 180);
-
                 break;
             case SHAPE_TRIANGLE:
-
                 objectAnimator1 = ObjectAnimator.ofFloat(shapeLoadingView, "rotation", 0, 180);
-
                 break;
         }

@@ -139,25 +164,24 @@
         animatorSet.addListener(new Animator.AnimatorListener() {
             @Override
             public void onAnimationStart(Animator animation) {
-
             }

             @Override
             public void onAnimationEnd(Animator animation) {
-                freeFall();
-
+                if (LoadingView.this.getVisibility() == View.VISIBLE) {
+                    freeFall();
+                }
             }

             @Override
             public void onAnimationCancel(Animator animation) {
-
             }

             @Override
             public void onAnimationRepeat(Animator animation) {
-
             }
         });
+        mAnimatorSet = animatorSet;
         animatorSet.start();

     }
@@ -180,27 +204,25 @@
         animatorSet.addListener(new Animator.AnimatorListener() {
             @Override
             public void onAnimationStart(Animator animation) {
-
             }

             @Override
             public void onAnimationEnd(Animator animation) {
-
-                shapeLoadingView.changeShape();
-                upThrow();
+                if (LoadingView.this.getVisibility() == View.VISIBLE) {
+                    shapeLoadingView.changeShape();
+                    upThrow();
+                }
             }

             @Override
             public void onAnimationCancel(Animator animation) {
-
             }

             @Override
             public void onAnimationRepeat(Animator animation) {
-
             }
         });
+        mAnimatorSet = animatorSet;
         animatorSet.start();
-
     }
 }

thanks very much

我今晚才发现有这个内存泄漏,明天再研究这位歪果朋友的方案,希望可行