错误画布:使用setDrawingCacheEnabled时尝试使用循环位图(false)

时间:2013-04-15 16:55:18

标签: android android-widget

我使用自定义布局,有时我启用和禁用绘图缓存。

唯一的问题是我正在开发最小的APIVersion 9,但是任何低于14的东西都会产生以下错误:

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)

我没有直接使用任何位图,我真的不确定问题是什么。如果我取出行设置setDrawingCacheEnabled(false),那么事情就会起作用,但我想保持它的性能。

1 个答案:

答案 0 :(得分:0)

好的,我解决了这个问题。我正在使用现有代码,但我认为它没有完全实现使用绘图缓存。我认为在现有视图上设置绘图缓存是这样的,并且视图将处理为您使用缓存,但看起来您实际上需要实现它。

我将此添加到我的自定义布局中,问题已消失;

@Override
public void draw(Canvas canvas) {
    if (cache != null && !cache.isRecycled()) {
        canvas.drawBitmap(cache, x, y, null);
    } else {
        super.draw(canvas);
    }
}