关于new Canvas(Bitmap)中Bitmap的isMutable的要求

今天在项目中通过

BitmapFactory.decodeResource(getResources(),R.drawable.demon,options);

给Canvas作为构造参数,结果报了一个错

这里写图片描述

因为:

在canvas类中你可以看到无论是构造方法还是setBitmap(Bitmap bitmap) 中都有

if (!bitmap.isMutable()) {
            throw new IllegalStateException(
                            "Immutable bitmap passed to Canvas constructor");
        }

就是说如果bitmap不可改变的情况下,canvas是不允许进行绘制的
当你用BitmapFactory.decodeResource,返回的bitmap是默认状态下的mIsMutable=false;(其他方法应该也是一样的,你自己可以看看)
Bitmap.createBitmap()中可以看到返回的是
Bitmap bm = nativeCreate(null, 0, width, width, height, config.nativeInt, true);这最后一个参数是true的也就是 mutable,说可以改变的位图

    private static native Bitmap nativeCreate(int[] colors, int offset,
                                              int stride, int width, int height,
                                            int nativeConfig, boolean mutable);

所以

传给Canvas的Bitmap 需要通过Bitmap.createBitmap()创建

你可能感兴趣的:(一些Bug,Android,异常)