我有几个逐帧动画 ( animation-list
)。将其中任何一个加载到 ImageView 都不是问题。当我尝试在同一个 ImageView 中加载不同的动画时,就会出现问题。
private void startAnimation(int anim) {
mImageView.setImageResource(anim);
((AnimationDrawable) mImageView.getDrawable()).start();
}
这是我正在使用的代码。在多次调用该函数后得到 java.lang.OutOfMemoryError
后,我添加了以下代码以尝试清除AnimationDrawable
和ImageView
。
private void startAnimation(int anim) {
if (mImageView.getDrawable() != null) { //trying to clear if it's not empty
if (((AnimationDrawable) mImageView.getDrawable()).isRunning()) {
((AnimationDrawable) mImageView.getDrawable()).stop();
((AnimationDrawable) mImageView.getDrawable()).selectDrawable(0);
}
mImageView.setImageResource(null);
}
//starting animation here
mImageView.setImageResource(anim);
((AnimationDrawable) mImageView.getDrawable()).start();
}
好吧,这没有用。我已经尝试过这里和这里发布的解决方案,但它们也没有奏效。我仍然不断得到OutOfMemoryError
。
这就是我调用startAnimation
函数的方式。
startAnimation(R.drawable.anim1);//works fine
startAnimation(R.drawable.anim2);//OutOfMemoryError
...
那么,如何释放内存并加载动画?请注意,我想一遍又一遍地这样做。