java.lang.IllegalArgumentException:无法绘制回收的位图 - Android

时间:2014-02-26 09:11:45

标签: java android image bitmap recycle

我正在尝试实施Android-wheel,这是一个可以用来模仿老虎机的库。

演示项目工作正常,它使用的是android自己的drawables => android.R.drawable。*

然而,当我将默认图像更改为我想要的图像时,它会给我这个错误:

java.lang.IllegalArgumentException: Cannot draw recycled bitmaps

我改变的是id的数组,其中包含对图像的引用

来自

 private final int items[] = new int[] {
                android.R.drawable.star_big_on,
                android.R.drawable.stat_sys_warning,
                android.R.drawable.radiobutton_on_background,
                android.R.drawable.ic_delete
        };

private final int items[] = new int[] {
                R.drawable.sunglasses, R.drawable.ball, R.drawable.box};

这是代码的其余部分。

private class SlotMachineAdapter extends AbstractWheelAdapter {
        // Image size
        final int IMAGE_WIDTH = 267;
        final int IMAGE_HEIGHT = 200;

        // Slot machine symbols
        private final int items[] = new int[] {
                R.drawable.sunglasses, R.drawable.ball, R.drawable.box};

        // Cached images
        private List<SoftReference<Bitmap>> images;

        // Layout inflater
        private Context context;

        /**
         * Constructor
         */
        public SlotMachineAdapter(Context context) {
            this.context = context;
            images = new ArrayList<SoftReference<Bitmap>>(items.length);
            for (int id : items) {
                images.add(new SoftReference<Bitmap>(loadImage(id)));
            }
        }

        /**
         * Loads image from resources
         */
         private Bitmap loadImage(int id) {
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), id);
        Bitmap scaled = Bitmap.createScaledBitmap(bitmap, IMAGE_WIDTH, IMAGE_HEIGHT, true);
        bitmap.recycle();
        return scaled;
    }

        @Override
        public int getItemsCount() {
            return items.length;
        }

        // Layout params for image view
        final LayoutParams params = new LayoutParams(IMAGE_WIDTH, IMAGE_HEIGHT);

        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            ImageView img;
            if (cachedView != null) {
                img = (ImageView) cachedView;
            } else {
                img = new ImageView(context);
            }
            img.setLayoutParams(params);
            SoftReference<Bitmap> bitmapRef = images.get(index);
            Bitmap bitmap = bitmapRef.get();
            if (bitmap == null) {
                bitmap = loadImage(items[index]);
                images.set(index, new SoftReference<Bitmap>(bitmap));
            }
            img.setImageBitmap(bitmap);

            return img;
        }
    }

这是完整的堆栈跟踪

    java.lang.IllegalArgumentException: Cannot draw recycled bitmaps
    at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:778)
    at android.view.GLES20RecordingCanvas.drawBitmap(GLES20RecordingCanvas.java:117)
    at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:393)
    at android.widget.ImageView.onDraw(ImageView.java:961)
    at android.view.View.draw(View.java:13765)
    at android.view.View.draw(View.java:13649)
    at android.view.ViewGroup.drawChild(ViewGroup.java:2940)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2810)
    at android.view.View.draw(View.java:13768)
    at kankan.wheel.widget.WheelView.drawItems(WheelView.java:587)
    at kankan.wheel.widget.WheelView.onDraw(WheelView.java:557)
    android.view.View.draw(View.java:13765)
    at android.view.View.getDisplayList(View.java:12716)
    at android.view.View.getDisplayList(View.java:12760)
    at android.view.View.draw(View.java:13489)
    at android.view.ViewGroup.drawChild(ViewGroup.java:2940)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2810)
    at android.view.View.getDisplayList(View.java:12714)
    at android.view.View.getDisplayList(View.java:12760)
    at android.view.View.draw(View.java:13489)
    at android.view.ViewGroup.drawChild(ViewGroup.java:2940)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2810)
    at android.view.View.draw(View.java:13768)
    at android.view.View.getDisplayList(View.java:12716)
    at android.view.View.getDisplayList(View.java:12760)
    at android.view.View.draw(View.java:13489)
    at android.view.ViewGroup.drawChild(ViewGroup.java:2940)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2810)
    at android.view.View.getDisplayList(View.java:12714)
    at android.view.View.getDisplayList(View.java:12760)
    at android.view.View.draw(View.java:13489)
    at android.view.ViewGroup.drawChild(ViewGroup.java:2940)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2810)
    at android.view.View.getDisplayList(View.java:12714)
    at android.view.View.getDisplayList(View.java:12760)
    at android.view.View.draw(View.java:13489)
    at android.view.ViewGroup.drawChild(ViewGroup.java:2940)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2810)
    at android.view.View.draw(View.java:13768)
    at android.widget.FrameLayout.draw(FrameLayout.java:467)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2394)
    at android.view.View.getDisplayList(View.java:12716)
    at android.view.View.getDisplayList(View.java:12760)
    at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1144)
    at android.view.ViewRootImpl.draw(ViewRootImpl.java:2267)
    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2139)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1950)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4464)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
    at android.view.Choreographer.doFrame(Choreographer.java:525)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)

at android.app.ActivityThread.main(ActivityThread.java:4895) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761) at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

我认为这是因为你太频繁地执行bitmap.recycle(),而system.gc()没有及时调用,所以变量位图是recycle(),但系统没有真正清除,我们可以&#39 ; t用它来加载新图像。所以,只需注释bitmap.recycle(),让系统自行回收gabage。

答案 1 :(得分:0)

试试这个 -

if(bitmap!=null)
   {
      bitmap.recycle();
      bitmap=null;
   }