如何使用 Canvas 创建巨大的白色位图?
我正在尝试弄清楚如何使用 Canvas 在大的白色表面上绘制一个小图形(实际上它是什么并不重要)。问题是,如果我从一个大的空位图开始,当我使用 ARGB_8888 创建它的可变副本时,Android 会立即耗尽内存。我很好奇我是否遗漏了一些东西,或者由于 Android 中的内存限制,是否实际上无法将小图形合成到大的白色表面上并将其保存为 PNG 或 JPG。
I'm trying to figure out how I can use Canvas to draw a small graphic (doesn't really matter what it is) onto a large white surface. The issue is that if I start with a large empty Bitmap, when I make a mutable copy of it using ARGB_8888 Android immediately runs out of memory. I'm curious if I'm missing something, or if it's actually not possible to composite a small graphic onto a large white surface and save it out as a PNG or JPG due to memory constraints in Android.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,当您想要创建巨大的位图时,您会受到内存的限制,但是您有足够的内存来创建相当大的位图。例如,1024*1024 ARGB_8888 位图将需要大约 4 MB 内存,如果您的应用程序通常使用内存比较节省,那么这不是问题。 Android 应用程序的正常堆大小通常在 16-32 MB 之间,具体取决于 Android 版本,只是为了让您了解必须使用的内容。
您说您制作了大位图的副本,这可能是您的主要问题。无需复制大位图,您只需要一个。下面是一个示例项目,它创建一个大的 (1024*1024) 白色位图,并在应用程序的中间绘制一个视图,然后将结果写入 PNG:
与此主布局一起:
您将在某处获得位图就像
/mnt/sdcard/Pictures/big-white-image-with-view.png
看起来像这样:Naturally, you are limited by memory when you want to create huge bitmaps, but you have enough memory to create quite big bitmaps. For example, a 1024*1024 ARGB_8888 bitmap will need roughly 4 MB of memory, which is not a problem if your app is frugal with memory in general. The normal heap size for an Android app is usually between 16-32 MB depending on Android version, just to give you a feeling for what you have to play with.
You say you make a copy of large bitmap, and that might be your main problem. There's no need to make a copy of a large bitmap, you need only one. Here's a sample project that creates a large (1024*1024) white bitmap and draws a View in your app in the middle of it, and then writes the result to a PNG:
Together with this main layout:
You'll get a bitmap somewhere like
/mnt/sdcard/Pictures/big-white-image-with-view.png
that looks something like this: