florent37 / ViewAnimator

A fluent Android animation library
Apache License 2.0
2.71k stars 423 forks source link

i got a memory leak with Animator$AnimatorListener #49

Closed LiveSalton closed 5 years ago

LiveSalton commented 5 years ago

my code is :

public static void viewScaleButtonAnimation(View... view) {
    ViewAnimator.animate(view).scale(1, 1.05f, 1).duration(3000)
            .repeatCount(-1).start();
}

how can i release this anim??

liyujiang-gzu commented 5 years ago
ViewAnimator viewAnimator = ViewAnimator.animate(view).scale(1, 1.05f, 1).duration(3000)
            .repeatCount(-1)
viewAnimator.start();

add method in ViewAnimator:

    public void pause() {
        if (this.animatorSet != null) {
            if (Build.VERSION.SDK_INT >= 19) {
                this.animatorSet.pause();
            }
        } else if (this.prev != null) {
            this.prev.pause();
        }
    }

    public void resume() {
        if (this.animatorSet != null) {
            if (Build.VERSION.SDK_INT >= 19) {
                this.animatorSet.resume();
            }
        } else if (this.prev != null) {
            this.prev.resume();
        }
    }

when onPause/onResume/onDestroy, try this:

viewAnimator.pause();
viewAnimator.resume();
viewAnimator.cance();

I don't know whether this can be solved.

liyujiang-gzu commented 5 years ago

如果通过pause、resume、cance可以解决内存泄露问题,那么,可以在ViewAnimator里加一个方法来自动维护生命周期,如:

public ViewAnimator attachLifecycle(LifecycleOwner owner) {
    owner.getLifecycle().addObserver(this);
    return this;
}
    @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
    public void onAny(LifecycleOwner source, Lifecycle.Event event) {
        if (event == Lifecycle.Event.ON_RESUME) {
            resume();
        } else if (event == Lifecycle.Event.ON_PAUSE) {
            pause();
        } else if (event == Lifecycle.Event.ON_DESTROY) {
            cance();
            source.getLifecycle().removeObserver(this);
        }
    }
LiveSalton commented 5 years ago

好的,非常感谢你的方法。我已经处理好内存泄漏的问题。

liyujiang-gzu commented 5 years ago

好的,非常感谢你的方法。我已经处理好内存泄漏的问题。

老铁,你是怎么处理的?

xmutzlq commented 2 years ago

泄漏怎么处理的?