使用动画onBackPressed中的内存泄漏

时间:2015-02-14 11:10:13

标签: android performance android-activity memory-leaks out-of-memory

我在开源应用程序中创建了一个相当复杂的结束动画。

如果调用onBackPressed()方法,应用程序将为视图设置动画并关闭。似乎有一个记忆韭菜,因为它后来不会停止分配新的ram。如果我删除动画并只是调用普通onBackPressed,我就不会有上述行为。

这是我在onBackPressed方法中调用的代码:

//make global var :D
ViewPropertyAnimator hideFabAnimator;

@Override
public void onBackPressed() {
    mFabDownloadButton.animate()
            .translationX(0)
            .setDuration(ANIMATION_DURATION_MEDIUM)
            .start();

    //move the share fab below the normal fab (58 because this is the margin top + the half
    mFabShareButton.animate()
            .translationX(0)
            .setDuration(ANIMATION_DURATION_MEDIUM)
            .setListener(new CustomAnimatorListener() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    //create the fab animation and hide fabProgress animation, set an delay so those will hide after the shareFab is below the main fab
                    hideFabAnimator = Utils.hideViewByScaleXY(mFabButton)
                            .setDuration(ANIMATION_DURATION_MEDIUM);

                    Utils.hideViewByScaleXY(mFabDownloadButton)
                            .setDuration(ANIMATION_DURATION_MEDIUM)
                            .start();
                    Utils.hideViewByScaleXY(mFabShareButton)
                            .setDuration(ANIMATION_DURATION_MEDIUM)
                            .start();
                    Utils.hideViewByScaleXY(mFabProgress)
                            .setDuration(ANIMATION_DURATION_MEDIUM)
                            .start();


                    //add listener so we can react after the animation is finished
                    hideFabAnimator.setListener(new CustomAnimatorListener() {

                        @Override
                        public void onAnimationEnd(Animator animation) {
                            try {
                                ViewPropertyAnimator hideFabAnimator = Utils.hideViewByScaleY(mTitleContainer);
                                hideFabAnimator.setListener(new CustomAnimatorListener() {
                                    @Override
                                    public void onAnimationEnd(Animator animation) {
                                        super.onAnimationEnd(animation);
                                        coolBack()
                                    }
                                });
                            } catch (Exception ex) {
                                super.onAnimationEnd(animation);
                                coolBack()
                            }
                        }
                    });

                    hideFabAnimator.start();
                    super.onAnimationEnd(animation);
                }
            })
            .start();
}

/**
 *
 */
private void coolBack() {
    try {
        super.onBackPressed();
    } catch (Exception e) {
    }
}

您可以在此处找到相同的源@github:https://github.com/mikepenz/wall-splash-android/blob/master/app/src/main/java/com/mikepenz/unsplash/activities/DetailActivity.java#L701

我尝试过很多不同的东西。但没有任何帮助。

感谢。

2 个答案:

答案 0 :(得分:1)

所以我能够解决这个问题。似乎如果你在没有animationListener的情况下使用普通动画,它可能会发生(它会发生)动画无法完成并且将保持" in-tact"。因此,即使活动已经关闭,它似乎仍在后台处理某些内容。

所以我能够通过定义一个监听器来解决这个问题 - "链"并检查我的所有动画是否完成。

以下是有效的代码:

@Override
public void onBackPressed() {
    mFabDownloadButton.animate()
            .translationX(0)
            .setDuration(ANIMATION_DURATION_MEDIUM)
            .setListener(animationFinishListener1)
            .start();


    //move the share fab below the normal fab (58 because this is the margin top + the half
    mFabShareButton.animate()
            .translationX(0)
            .setDuration(ANIMATION_DURATION_MEDIUM)
            .setListener(animationFinishListener1)
            .start();
}

private CustomAnimatorListener animationFinishListener1 = new CustomAnimatorListener() {
    private int animateFinish1 = 0;

    @Override
    public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        process();
    }

    @Override
    public void onAnimationCancel(Animator animation) {
        super.onAnimationCancel(animation);
        process();
    }

    private void process() {
        animateFinish1 = animateFinish1 + 1;
        if (animateFinish1 == 2) {
            //create the fab animation and hide fabProgress animation, set an delay so those will hide after the shareFab is below the main fab
            Utils.hideViewByScaleXY(mFabDownloadButton)
                    .setDuration(ANIMATION_DURATION_MEDIUM)
                    .setListener(animationFinishListener2)
                    .start();
            Utils.hideViewByScaleXY(mFabShareButton)
                    .setDuration(ANIMATION_DURATION_MEDIUM)
                    .setListener(animationFinishListener2)
                    .start();
            Utils.hideViewByScaleXY(mFabProgress)
                    .setDuration(ANIMATION_DURATION_MEDIUM)
                    .setListener(animationFinishListener2)
                    .start();
            Utils.hideViewByScaleXY(mFabButton)
                    .setDuration(ANIMATION_DURATION_MEDIUM)
                    .setListener(animationFinishListener2)
                    .start();
        }
    }
};

private CustomAnimatorListener animationFinishListener2 = new CustomAnimatorListener() {
    private int animateFinish2 = 0;

    @Override
    public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        process();
    }

    @Override
    public void onAnimationCancel(Animator animation) {
        super.onAnimationCancel(animation);
        process();
    }

    private void process() {
        animateFinish2 = animateFinish2 + 1;
        if (animateFinish2 == 4) {
            ViewPropertyAnimator hideFabAnimator = Utils.hideViewByScaleY(mTitleContainer);
            hideFabAnimator.setListener(new CustomAnimatorListener() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    coolBack();
                }
            });
        }
    }
};

/**
 *
 */
private void coolBack() {
    try {
        super.onBackPressed();
    } catch (Exception e) {
        // ew;
    }
}

所以我会继续计算我的动画,并且只会在第一个"批次"动画完成后,我将继续第二批"批次"动画。一旦完成,我将完成活动。

答案 1 :(得分:0)

我有一个动画的类似泄漏。在我的情况下,我使用

scaleY.setRepeatCount(1);
scaleY.setRepeatMode(ValueAnimator.REVERSE);

for setRepeatCount我之前使用过INFINITY,它也从不用于动画,甚至在调用显式的cancle()或end()之后也没有。我只是将其改为1(它不再是无限的,但我可以忍受它)。 刚刚添加了这个答案,也有人遇到了这个问题。