AnimationDrawable:释放内存的正确方法

时间:2015-07-08 06:47:16

标签: android animationdrawable

使用AnimationDrawable时,释放内存的正确方法是什么?现在我正在使用:

for (int frameCounter = 0; frameCounter < animation.getNumberOfFrames(); ++frameCounter) {
    Drawable frame = animation.getFrame(frameCounter);
    if (frame instanceof BitmapDrawable) {
        BitmapDrawable castedFrame = (BitmapDrawable) frame;
        Bitmap bitmap = castedFrame.getBitmap();
        if (bitmap != null && !bitmap.isRecycled())
            bitmap.recycle();
    }
    frame.setCallback(null);
}
animation.setCallback(null);

动画是我的AnimationDrawable。问题是,当我回到相同的活动(因此再次显示动画)时,会抛出异常:

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@2ee1a817
    at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1225)
    at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:600)
    at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:544)
    at android.graphics.drawable.DrawableContainer.draw(DrawableContainer.java:76)
    at android.widget.ImageView.onDraw(ImageView.java:1187)
    at android.view.View.draw(View.java:16269)
    at android.view.View.updateDisplayListIfDirty(View.java:15192)
    at android.view.View.getDisplayList(View.java:15215)
    at android.view.View.draw(View.java:15990)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3708)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3502)
    at android.view.View.updateDisplayListIfDirty(View.java:15187)
    at android.view.View.getDisplayList(View.java:15215)
    at android.view.View.draw(View.java:15990)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3708)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3502)
    at android.view.View.updateDisplayListIfDirty(View.java:15187)
    at android.view.View.getDisplayList(View.java:15215)
    at android.view.View.draw(View.java:15990)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3708)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3502)
    at android.view.View.updateDisplayListIfDirty(View.java:15187)
    at android.view.View.getDisplayList(View.java:15215)
    at android.view.View.draw(View.java:15990)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3708)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3502)
    at android.view.View.draw(View.java:16272)
    at android.widget.FrameLayout.draw(FrameLayout.java:592)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:3047)
    at android.view.View.updateDisplayListIfDirty(View.java:15192)
    at android.view.View.getDisplayList(View.java:15215)
    at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:307)
    at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:313)
    at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:352)
    at android.view.ViewRootImpl.draw(ViewRootImpl.java:2811)
    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2634)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2222)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1230)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6726)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
    at android.view.Choreographer.doCallbacks(Choreographer.java:590)
    at android.view.Choreographer.doFrame(Choreographer.java:560)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6141)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

有趣的是,这次异常不是第二次我参加活动,而是第三次。有任何想法吗?感谢

0 个答案:

没有答案