将位图添加到 Canvas 时出现 IllegalStateException

发布于 2024-11-06 15:05:27 字数 593 浏览 0 评论 0原文

我试图使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白云不回头 2024-11-13 15:05:27

可能会引发 IllegalStateException,因为您正在加载位图 (eBit) 并使用 mCanvas.setBitmap(eBit) 而不检查位图是否可变。这是在位图上绘制所必需的。要确保您的位图是可变的,请使用:

eBit=LoadBMPsdcard(filePath);
Bitmap bitmap = eBit.copy(Bitmap.Config.ARGB_8888, true);
canvas.setBitmap(bitmap);

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:

eBit=LoadBMPsdcard(filePath);
Bitmap bitmap = eBit.copy(Bitmap.Config.ARGB_8888, true);
canvas.setBitmap(bitmap);
听风念你 2024-11-13 15:05:27

尝试使用 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.

秋千易 2024-11-13 15:05:27

当且仅当 Bitmap.isMutable() 返回 true 时,Canvas.setBitmap() 才会抛出 IllegalStateExceptionBitmap.createBitmap() 仅以所有形式构建不可变的 Bitmap 实例。要创建可变位图,您可以使用 new Bitmap()Bitmap.copy(true),具体取决于您是否有要开始使用的源位图。对我来说,一个典型的块看起来像:

Bitmap image = ...
Canvas c = new Canvas(image.isMutable()?image:image.copy(true));
...

当然,这假设您不介意破坏源位图(我通常不介意,但这绝不是通用的)。

Canvas.setBitmap() throws IllegalStateException if and only if Bitmap.isMutable() returns true. Bitmap.createBitmap() builds an immutable Bitmap instance only, in all of its forms. To create a mutable bitmap you either use new Bitmap(), or Bitmap.copy(true), depending on whether you have a source bitmap that you want to start with. A typical block for me looks like:

Bitmap image = ...
Canvas c = new Canvas(image.isMutable()?image:image.copy(true));
...

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文