Reputation: 509
I'm using Animation drawable to display a gif. I split the gif into frames, I created an animation file and I load it using:
animationImageView.setImageResource(splashAnimationResource);
animation = (AnimationDrawable) animationImageView.getDrawable();
All this happens inside onResume(). After the animation has been displayed, inside onPause() I try to free memory:
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);
This all works very nice, animation is displayed correctly and memory is freed. If I try to go to the animation activity again, first time it works, but then it throws an exception:
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)
I am really confused. Any ideas?
Upvotes: 1
Views: 703
Reputation: 2337
I found the solution after banging my head for a couple of days. First you need to create smaller images size, for instance I was first using 400x400 size frames.. then i reduced to 100x100. Then i removed all the codes for bitmap recycling as there isn't any solution for this problem. Then u just play with animation.start()
and .stop()
and it would be really good.
Upvotes: 3