在android中将画布转换为位图图像
我正在尝试在画布上开发一个应用程序,我正在画布上绘制位图。绘制后,我尝试将其转换为位图图像。
谁能给我一个建议吗?
I am trying to develop an app on canvas,I am drawing a bitmap on the canvas. After drawing, I am trying to convert into bitmap image.
Can anyone give me a suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
建议取决于您想要做什么。
如果您担心控件需要很长时间才能绘制,并且想要绘制位图,以便可以位图位图传输而不是通过画布重新绘制,那么您不想要双重猜测平台 - 控件自动将其绘图缓存到临时位图,甚至可以使用
getDrawingCache()
如果你想使用画布绘制位图,通常的方法是:
Bitmap.createBitmap()
Canvas(Bitmap)
构造函数Advice depends upon what you are trying to do.
If you are concerned that your controls take a long time to draw, and you want to draw to a bitmap so you can blit the bitmap rather than re-drawing via a canvas, then you don't want to be double-guessing the platform - controls automatically cache their drawing to temporary bitmaps, and these can even be fetched from the control using
getDrawingCache()
If you want to draw using a canvas to a bitmap, the usual recipe is:
Bitmap.createBitmap()
Canvas(Bitmap)
constructor因此,您创建一个新的
Bitmap
,例如:Bitmap myBitmap = new Bitmap( (int)Width, (int)Height, Config.RGB_565 )
和
width< /code> 和
height
与您的画布相同。接下来,使用canvas.setBitmap(myBitmap),但不使用drawBitmap()。
调用
setBitmap
后,您在画布上绘制的所有内容实际上都是按照我所演示的示例代码在myBitmap
上绘制的。编辑:
您不能直接创建位图,例如:
您必须使用:
So you create a new
Bitmap
, for example:Bitmap myBitmap = new Bitmap( (int)Width, (int)Height, Config.RGB_565 )
with
width
andheight
being the same as your canvas.Next, use
canvas.setBitmap(myBitmap)
, but notdrawBitmap()
.After you call
setBitmap
, all what you draw on canvas is in fact, drawing on yourmyBitmap
going by the example code I have illustrated.Edit:
You can not create a bitmap directly such as:
You must use instead:
其他例子:
Other example:
以下是将画布转换为位图并将其存储到图库或特定文件夹的步骤。
注意:请确保您已授予 WRITE_EXTERNAL_STORAGE
activity_main.xml
MainActivity.java
创建父布局的引用
将其存储到图库中
转换为位图
Following are the steps to convert from canvas to bitmap and storing it to gallery or specific folder.
Note: Make sure you have given permission of WRITE_EXTERNAL_STORAGE
activity_main.xml
MainActivity.java
Create reference of parent layout
To store it into gallery
To convert into bitmap