android animatorset内存管理,从imageview中释放内存

时间:2013-09-05 11:39:31

标签: android android-animation android-memory

我有一个重复的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 :(得分: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();

        }

    });
}