将位图添加到 Canvas 时出现 IllegalStateException
我试图使用 setBitMap
将位图图像设置到画布上,当时我收到了 IllegalStateException。此画布上当前有一些图像,我正在尝试替换它。 任何人都知道为什么会发生这种情况?
代码片段
editBm = Bitmap.createBitmap(951, 552, Bitmap.Config.ARGB_8888);
Canvas mCanvas=new Canvas(editBm);
eBit=LoadBMPsdcard(filePath); ---->returns a bitmap when the file path to the file is provided
Log.i("BM size", editBm.getWidth()+"");
mCanvas.setBitmap(eBit);
我没有收到任何 NullPointer 错误,并且方法 LoadBMPsdcard()
运行良好。
请让我知道您的任何想法...
提前致谢
快乐编码
I was trying to set a bitmap image to a canvas using setBitMap
,at that time I got an IllegalStateException.This canvas have some images on it currently, I am trying to replace it.
Any one have any idea why this happened?
Code Snippet
editBm = Bitmap.createBitmap(951, 552, Bitmap.Config.ARGB_8888);
Canvas mCanvas=new Canvas(editBm);
eBit=LoadBMPsdcard(filePath); ---->returns a bitmap when the file path to the file is provided
Log.i("BM size", editBm.getWidth()+"");
mCanvas.setBitmap(eBit);
I am not getting any NullPointer errors and the method LoadBMPsdcard()
is working good.
Please let me know about any ideas you have ...
Thanks in advance
Happy Coding
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能会引发 IllegalStateException,因为您正在加载位图 (eBit) 并使用
mCanvas.setBitmap(eBit)
而不检查位图是否可变。这是在位图上绘制所必需的。要确保您的位图是可变的,请使用:IllegalStateException could be thrown because you're loading a Bitmap (eBit) and use
mCanvas.setBitmap(eBit)
without checking if the bitmap is mutable. This is requiered to draw on the Bitmap. To make sure your Bitmap is mutable use:尝试使用 drawBitmap 而不是 setBitmap。看起来您已经通过将位图传递给画布构造函数来设置要绘制的位图,因此现在您只需将所有内容绘制到其上即可。
Try to use drawBitmap instead of the setBitmap. It looks like you've already set a bitmap to draw into by passing it to the canvas constructor, so now you just need to draw everything onto it.
当且仅当
Bitmap.isMutable()
返回 true 时,Canvas.setBitmap()
才会抛出IllegalStateException
。Bitmap.createBitmap()
仅以所有形式构建不可变的 Bitmap 实例。要创建可变位图,您可以使用new Bitmap()
或Bitmap.copy(true)
,具体取决于您是否有要开始使用的源位图。对我来说,一个典型的块看起来像:当然,这假设您不介意破坏源位图(我通常不介意,但这绝不是通用的)。
Canvas.setBitmap()
throwsIllegalStateException
if and only ifBitmap.isMutable()
returns true.Bitmap.createBitmap()
builds an immutable Bitmap instance only, in all of its forms. To create a mutable bitmap you either usenew Bitmap()
, orBitmap.copy(true)
, depending on whether you have a source bitmap that you want to start with. A typical block for me looks like:This assumes, of course, that you don't mind clobbering the source Bitmap (which I generally don't but that's by no means universal).