Android 自定义视图将可见屏幕保存为位图
在我的应用程序中,用户操作图像,旋转,捏合,缩放等。这是在 onDraw()
using canvas.translate()
, canvas.rotate 中完成的()
等。当然,图像很大,在操作图像后,用户会与其他视图进行交互。问题是我不想一遍又一遍地重新绘制整个大图像,只绘制可见部分。所以我想,在用户完成操作后,将屏幕上当前的可见图像保存到内存/位图/缓存中,然后仅重绘该图像。
我怎样才能做到这一点?
In my app the user manipulates a image, rotates, pinch, zoom etc. This is make within onDraw()
using canvas.translate()
, canvas.rotate()
etc. Of course the image is big and after the manipulation of the image the user performs interactions with other Views. The thing is I don't want to redraw the whole big image over and over again only the visible part. So I'd like, after the user finishes operations, SAVE the current VISIBLE image on the screen to memory/bitmap/cache and redraw only that.
How can I achieve this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在自定义视图的构造函数中,启用绘图缓存,如下所示:
setDrawingCacheEnabled(true);
。然后,每当您想要保存Canvas
的状态时,您都可以调用getDrawingCache()
,它会返回所需的Bitmap
。文档:getDrawingCache()
In the constructor of your custom View, enable the drawing cache like this:
setDrawingCacheEnabled(true);
. Then whenever you want to save the state of yourCanvas
you can callgetDrawingCache()
, which returns the desiredBitmap
.Documentation: getDrawingCache()
在绘制时用位图初始化画布。
保留此位图的引用并在需要时保存它。
In your on draw initialize your Canvas with a bitmap.
keep a reference of this bitmap and save it whenever required.
我找到了一篇关于我需要什么的非常好的文章 这里
为了让事情变得清晰,首先我需要创建一个具有视图大小的位图,然后为其创建一个画布,在画布上绘制,然后保存它。
我只需在需要时在 onDraw() 上调用它即可。它将在内存中保存一个位图,我可以用它来绘制,而不是大图片。
I've found a really great article about what I needed HERE
To kepp thing clear, first I need to create a bitmap with the size of my View, then create a canvas for it, draw on canvas and then save it.
I simply call this on onDraw() when I need it. It will save a bitmap on internal memory which I can use to draw, instead of the big picture.
以下内容可能会对您有所帮助:
(1)
(2) 这与 如何捕获 Android 设备屏幕内容?
(3) 你可以尝试使用这个库
http://code.google.com/p/android-screenshot-library/< /a> 它引入了 Android 屏幕截图库 (ASL),可以通过编程方式从 Android 设备捕获屏幕截图,而无需具有 root 访问权限。
Following may help you :
(1)
(2) This is a similar to another question at How to capture the android device screen content?
(3) You can try using this library
http://code.google.com/p/android-screenshot-library/ It introduces an Android Screenshot Library (ASL) which enables to programmatically capture screenshots from Android devices without requirement of having root access privileges.