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.
Share
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.pngthat looks something like this: