我使用动画drawable来显示gif。我将gif分成帧,我创建了一个动画文件,然后使用:
加载它animationImageView.setImageResource(splashAnimationResource);
animation = (AnimationDrawable) animationImageView.getDrawable();
所有这些都发生在onResume()中。显示动画后,在onPause()内部我尝试释放内存:
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);
}
splashAnimation.setCallback(null);
这一切都很好,动画显示正确,内存被释放。如果我再次尝试进入动画活动,第一次它可以工作,但它会引发异常:
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 :(得分:3)
我在敲了几天之后找到了解决方案。首先你需要创建更小的图像尺寸,例如我首先使用400x400尺寸的框架..然后我减少到100x100。然后我删除了所有的位图回收代码,因为没有任何解决方案来解决这个问题。然后,您只需使用int_questSol_int_int
和animation.start()
,这将非常棒。