我有一个重复的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 */
}