3

我有几个逐帧动画 ( 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();
}

好吧,这没有用。我已经尝试过这里这里发布的解决方案,但它们也没有奏效。我仍然不断得到OutOfMemoryError

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

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

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

4

1 回答 1

0

我终于mImageView.getDrawable().setVisible(false, true)在开始新动画之前添加了它。

于 2016-06-24T19:51:07.613 回答