0

Im using a custom layout and at times I enable and disable the drawing cache.

The only gotcha is that I'm developing for min APIVersion 9, but anything below 14 produces the following error:

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@41ee8a88
E/AndroidRuntime( 6954):    at android.graphics.Canvas.throwIfRecycled(Canvas.java:1026)
E/AndroidRuntime( 6954):    at android.graphics.Canvas.drawBitmap(Canvas.java:1065)
E/AndroidRuntime( 6954):    at android.view.View.draw(View.java:13620)
E/AndroidRuntime( 6954):    at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
E/AndroidRuntime( 6954):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
E/AndroidRuntime( 6954):    at android.view.View.draw(View.java:13589)
E/AndroidRuntime( 6954):    at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
E/AndroidRuntime( 6954):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
E/AndroidRuntime( 6954):    at android.view.View.draw(View.java:13710)
E/AndroidRuntime( 6954):    at android.widget.FrameLayout.draw(FrameLayout.java:467)
E/AndroidRuntime( 6954):    at android.view.View.draw(View.java:13591)
E/AndroidRuntime( 6954):    at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
E/AndroidRuntime( 6954):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
E/AndroidRuntime( 6954):    at android.view.View.draw(View.java:13589)
E/AndroidRuntime( 6954):    at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
E/AndroidRuntime( 6954):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
E/AndroidRuntime( 6954):    at android.view.View.draw(View.java:13710)
E/AndroidRuntime( 6954):    at android.widget.FrameLayout.draw(FrameLayout.java:467)
E/AndroidRuntime( 6954):    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2211)
E/AndroidRuntime( 6954):    at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2281)
E/AndroidRuntime( 6954):    at android.view.ViewRootImpl.draw(ViewRootImpl.java:2177)
E/AndroidRuntime( 6954):    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045)
E/AndroidRuntime( 6954):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
E/AndroidRuntime( 6954):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
E/AndroidRuntime( 6954):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
E/AndroidRuntime( 6954):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
E/AndroidRuntime( 6954):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
E/AndroidRuntime( 6954):    at android.view.Choreographer.doFrame(Choreographer.java:532)
E/AndroidRuntime( 6954):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
E/AndroidRuntime( 6954):    at android.os.Handler.handleCallback(Handler.java:725)
E/AndroidRuntime( 6954):    at android.os.Handler.dispatchMessage(Handler.java:92)

I'm not directly using any bitmaps, I'm really not sure what the problem is. If I take out the line setting setDrawingCacheEnabled(false), things work, but I'd like to keep it in for performance.

4

1 回答 1

0

Okay I fixed the problem. I was using existing code, but I don't think it had been fully implemented to use the drawing cache. I thought that setting the drawing cache on an existing view was it, and that the view would deal with using the cache for you, but it seems like you actually need to implement it.

I added this to my custom layout and the problem has dissapeared;

@Override
public void draw(Canvas canvas) {
    if (cache != null && !cache.isRecycled()) {
        canvas.drawBitmap(cache, x, y, null);
    } else {
        super.draw(canvas);
    }
}
于 2013-04-16T14:14:52.590 回答