【问题标题】:OutOfMemoryError when trying to load multiple frame-by-frame animations one after the other in Android尝试在Android中一个接一个地加载多个逐帧动画时出现OutOfMemoryError
【发布时间】:2016-06-23 19:26:13
【问题描述】:

我有几个逐帧动画 (animation-list)。将其中任何一个加载到 ImageView 都不是问题。当我尝试在同一个 ImageView 中加载不同的动画时会出现此问题。

private void startAnimation(int anim) {
    mImageView.setImageResource(anim);
    ((AnimationDrawable) mImageView.getDrawable()).start();
}

这是我使用的代码。在多次调用该函数后得到java.lang.OutOfMemoryError后,我添加了以下代码尝试清除AnimationDrawableImageView

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();
}

好吧,这没用。我已经尝试过发布herehere 的解决方案,但它们也不起作用。我仍然不断收到OutOfMemoryError

这就是我调用startAnimation 函数的方式。

startAnimation(R.drawable.anim1);//works fine
startAnimation(R.drawable.anim2);//OutOfMemoryError
 ...

那么,如何释放内存并加载动画?请注意,我想一遍又一遍地这样做。

【问题讨论】:

  • 我认为只有垃圾收集器才能在应用加载新资源时删除旧动画资源。
  • 我也试过System.gc() 无济于事。你有什么建议?
  • 对不起,我没有电脑来寻找解决方案。我只写了一个可能的错误原因。

标签: android imageview out-of-memory animationdrawable


【解决方案1】:

我终于通过在开始新动画之前添加mImageView.getDrawable().setVisible(false, true) 来让它工作。

【讨论】:

    猜你喜欢
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多