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 */
}
4

1 回答 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();

        }

    });
}
于 2013-09-05T11:51:28.290 回答