在画布上绘图并保存图像
我是 Android 图形类的新手。我想使用触摸事件绘制图像(实际上是签名类型),并希望在保存时将其保存在 SD 卡上。我已经在网上扫描过任何此类教程,但没有找到。谁能告诉我如何使用触摸事件在画布上绘图并保存它。
任何教程或示例代码都会有很大帮助。
I am new to the Android Graphics class. I want to draw an image(actually a signature kind) using the touch events and want it to be saved on SDcard when I want to save it. I have scanned through the web for any such tutorials but I have not found any. Can anyone please tell me how to draw on canvas using the touch events and save it.
Any tutorials or sample code will be of great help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在android开发人员上看到了非常好的代码,但我再也找不到它了......它的输出是贝塞尔曲线,所以它会非常平滑。这是我编辑的代码:
然后在活动的 onCreate 中,您想使用它,只需编写如下内容:
此视图是透明的,并使用黑色油漆用手指绘制。因此,如果您想查看所绘制的内容,只需在此视图的背景上绘制白色或灰色位图(只需在 onDraw 的开头添加一行),或者您可以使用父级的背景。
然后,当您想从您绘制的图像创建图像时,您只需调用
它取决于您想要作为输出的内容,您可以使用此代码,或者代替 parent 您可以使用 parent 来执行此操作>myDrawView 并且您只得到您绘制的没有背景的图像(因为我们的 myDrawView 背景是透明的)。
希望这会有所帮助。请随时留下反馈。
I saw really good code on android developers, but I can't find it anymore... It's output is bezier curves so it will be pretty smooth. Here is code that I edited:
then in onCreate of activity you want to use it in you just write something like this:
This view is transparent and uses black paint to draw with your finger. So if you want see what you draw simply draw a white or gray bitmap on background of this view (you just add one line in the beginnig of onDraw), or you can use the background of the parent.
Then when you want to create an image from what you have drawn you just call
It depends on what you want to have as output, you can use this code or instead of parent you can do this with myDrawView and you get just the image you have drawn without background (since we have our myDrawView background transparent).
Hope this will help. Feel free to leave feedback.
绘图的东西
Scribbler.java:
DrawView.java:
AndroidManifest.xml:
..会给你类似的东西:
您可能想要绘制线条而不是像素
保存的东西
要实现保存,您可以将
位图
传递到Canvas
构造函数,然后使用压缩Bitmap
方法创建一个OutputStream
,可以将其写入 SD 卡编辑以回答您的评论:
当然,您可以使用 XML 来定义布局,因为
DrawView
扩展了 View,您可以在布局 xml 文件中使用它。main.xml
布局文件的示例可为您提供类似的内容:
您只需要一个额外的构造函数
public DrawView(Context context, AttributeSet attrSet)
the drawing thing
Scribbler.java:
DrawView.java:
AndroidManifest.xml:
..will give you something like that:
You may want to draw lines instead of pixels
the saving thing
To implement saving you can pass a
Bitmap
into theCanvas
constructor, then use the compressmethod of
Bitmap
to create anOutputStream
which can be written to the SD cardEDIT to answer your comment:
Sure you can use XML to define your layout since
DrawView
extends View you can use it in your layout xml file.An example for the
main.xml
layout filethat gives you something like that:
You'll just need an additional contstructor
public DrawView(Context context, AttributeSet attrSet)
我设置了一个绘图视图类,并且执行 Belovoj 建议的操作在该类中也有效:
I had a drawing view class set up, and doing what Belovoj suggested worked also in that class: