在位图上写注释

发布于 2024-10-03 09:00:06 字数 312 浏览 2 评论 0原文

我在 ImageView 上显示了一个位图,现在我想提供一个工具来在该位图上编写用户输入的注释。

我尝试使用

 Canvas canvas = new Canvas(srcBitmap); canvas.drawText("Hello", 100,100,null);

但这给了我以下错误

java.lang.IllegalStateException: 传递给 Canvas 的不可变位图 构造函数

稍后我想将整个图像保存为位

I have a bitmap displayed on ImageView now i want to give a facility to write comment typed by the user on that bitmap.

i tried using

 Canvas canvas = new Canvas(srcBitmap); canvas.drawText("Hello", 100,100,null);

but this is giving me following error

java.lang.IllegalStateException:
Immutable bitmap passed to Canvas
constructor

later on i want to save this whole image a bitmap

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

安静被遗忘 2024-10-10 09:00:06

你从哪里得到你的位图?从异常来看,这意味着您正在直接使用无法修改的资源/资产(它在实际的 apk 中)。为了避免这种情况,您需要复制位图并将其用于画布。这里有一些示例可供使用。

Where did you get your bitmap from? From the exception it means that you are using a resource/asset directly which can not be modified (it is in the actual apk). To avoid this you need to make a copy of the bitmap and use it for the canvas. Here you got some examples to work with.

手心的温暖 2024-10-10 09:00:06

正如莫斯指出的,位图需要是可变的。这是一些源代码,您可以如何做到这一点:

//first, get bitmap and make it mutable
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
//now, create canvas and paint as you like
Canvas canvas = new Canvas(mutableBitmap);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(5);
canvas.drawLine(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
//finally, convert back to icon
Drawable icon = new BitmapDrawable(context.getResources(), mutableBitmap);
getSupportActionBar().setIcon(icon);

As Moss pointed out, the bitmap needs to be mutable. Here is some source code how you can do it:

//first, get bitmap and make it mutable
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
//now, create canvas and paint as you like
Canvas canvas = new Canvas(mutableBitmap);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(5);
canvas.drawLine(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
//finally, convert back to icon
Drawable icon = new BitmapDrawable(context.getResources(), mutableBitmap);
getSupportActionBar().setIcon(icon);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文