Android AnimatorSet 内存管理:从 ImageView 中释放内存

3

我有一个重复的AnimatorSet,在几个不同的活动中使用一系列全帧图像。 由于似乎图像视图占用了太多内存,我得到了内存不足异常。

最初,后台中的活动仍在循环播放它们的动画,消耗内存。 我尝试通过在暂停/恢复时创建和结束动画来解决问题,以便从非前景视图中的动画中回收内存,但我认为我需要更多地清理图像视图中的内存。

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_face_o_matic_start);
        getActionBar().hide();

}

@Override
protected void onResume() {
    super.onResume();
    createAnimations();
}


@Override
protected void onPause() {
    super.onPause();
    this.mAnimatorSet.removeAllListeners();
    this.mAnimatorSet.end();
            //TODO: clear and free up the memory from the image views somehow
            // this is the part I need help with I think

}

protected ObjectAnimator getGlowAnimation(View v, int duration){
    ObjectAnimator animation = ObjectAnimator.ofFloat(v, "alpha", 0f, 1f);
    animation.setDuration(duration);
    animation.setRepeatCount(1);
    animation.setRepeatMode(Animation.REVERSE);
    return animation;
}

public void createAnimations(){

    //TODO: destroy these imageviews to reclaim memory on pause
            // originally had them in on create but the imageviews continued to
            // take up memory when the activity went into the background
    /*start animations */

    final ImageView bg1 = (ImageView) findViewById(R.id.bg_face_o_matic_1);
    final ImageView bg2 = (ImageView) findViewById(R.id.bg_face_o_matic_2);
    final ImageView bg3 = (ImageView) findViewById(R.id.bg_face_o_matic_3);
    final ImageView bg4 = (ImageView) findViewById(R.id.bg_face_o_matic_4);
    final ImageView bg5 = (ImageView) findViewById(R.id.bg_face_o_matic_5);
    final ImageView bg6 = (ImageView) findViewById(R.id.bg_face_o_matic_6);

    ObjectAnimator anim1 = getGlowAnimation(bg1, 400);
    ObjectAnimator anim2 = getGlowAnimation(bg2, 400);      
    ObjectAnimator anim3 = getGlowAnimation(bg3, 400);
    ObjectAnimator anim4 = getGlowAnimation(bg4, 400);
    ObjectAnimator anim5 = getGlowAnimation(bg5, 400);
    ObjectAnimator anim6 = getGlowAnimation(bg6, 400);

    List<Animator> animations = new ArrayList<Animator>(Arrays.asList(anim1, anim2, anim3, anim4, anim5, anim6));
    mAnimatorSet.playSequentially(animations);
            //loop the animator set
    mAnimatorSet.addListener(new AnimatorListenerAdapter(){

        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            mAnimatorSet.start();

        }

    });

    mAnimatorSet.start();
    /*end animations */
}
1个回答

0
每次您的应用程序进入此Activity时,它都会调用您的onResume()方法,在其中调用createAnimations(),因此它不断地创建大量图像和其他内容。您应该在此方法之外声明一些内容,将它们声明为类级别并在onCreate()中进行初始化,这样效率会更高。我稍微减少了您的createAnimations()。只需在初始化其必要的依赖变量后在onCreate()中调用此方法一次,在onResume()中只需调用mAnimatorSet.playSequentially(animations);即可。
public void createAnimations(){


    mAnimatorSet.playSequentially(animations);
            //loop the animator set
    mAnimatorSet.addListener(new AnimatorListenerAdapter(){

        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            mAnimatorSet.start();

        }

    });
}

我应该怎么做来释放内存?最初,我将imageviews等内容放在oncreate中,但是在暂停时它从未释放内存。我将imageviews的初始化移动到了onresume中,想着我会在暂停时销毁imageviews。但是我还没有开始销毁图像,所以只完成了一半。我应该怎么做来释放imageviews和object animators上的图像,在暂停时? - MonkeyBonkey
有一个用于图像的recycle()方法,但频繁进行allocate/deallocate会使情况变得更糟。我建议在onCreate中初始化它们,因为你一直在使用它们而没有改变,所以只需初始化一次即可解决问题。不要在onResume()中初始化它们,因为它将被多次调用,这样做会比受益更少。 - Onur A.
我的意思是它只恢复一次,因此它会在创建时显示,并且在从Facebook登录返回后,只有一个活动具有恢复事件。 - MonkeyBonkey
啊,那么,请不要使用recycle()方法,因为它会认为您不需要再次绘制该位图,从而导致错误“Canvas: trying to use a recycled bitmap”。另外,您的图片分辨率很大吗?有关位图内存的详细指南,请参阅http://developer.android.com/training/displaying-bitmaps/manage-memory.html。 - Onur A.
这些图片是全幅的,意味着它们占据整个安卓星系背景,是全尺寸的。 - MonkeyBonkey
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接