尝试在 Android 中一个接一个地加载多个逐帧动画时出现 OutOfMemoryError
OutOfMemoryError when trying to load multiple frame-by-frame animations one after the other in Android
我有几个逐帧动画 (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();
}
好吧,这没用。我已经尝试了 here and here 发布的解决方案,但它们也没有用。我仍然不断收到 OutOfMemoryError
。
这就是我调用 startAnimation
函数的方式。
startAnimation(R.drawable.anim1);//works fine
startAnimation(R.drawable.anim2);//OutOfMemoryError
...
那么,如何释放内存并加载动画?请注意,我想一遍又一遍地这样做。
我终于通过在开始新动画之前添加 mImageView.getDrawable().setVisible(false, true)
使其工作。
我有几个逐帧动画 (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();
}
好吧,这没用。我已经尝试了 here and here 发布的解决方案,但它们也没有用。我仍然不断收到 OutOfMemoryError
。
这就是我调用 startAnimation
函数的方式。
startAnimation(R.drawable.anim1);//works fine
startAnimation(R.drawable.anim2);//OutOfMemoryError
...
那么,如何释放内存并加载动画?请注意,我想一遍又一遍地这样做。
我终于通过在开始新动画之前添加 mImageView.getDrawable().setVisible(false, true)
使其工作。