Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
619 views
in Technique[技术] by (71.8m points)

java - Bitmap/Canvas use and the NDK

I've recently found out that there is no hard limit to the amount of memory NDK code can allocate in contrast to the heavily limited amount of memory (~25Mb on most devices) you can allocate on the Java side.

I want to write an image processing app (something like Photoshop) that needs to keep several large bitmaps in memory at once where the bitmap data will take ~20Mb of memory. Doing this in Java makes the app prone to out of memory exceptions on many devices I've tried.

All my current code makes use of the Bitmap and Canvas class for doing my image manipulations. Can anyone suggest some way that allows me to allocate most of my memory on the C side and still make use of Bitmap+Canvas for performing my drawing operations (using Android 2.1 and above)?

As an example, if my image is composed of 6 bitmap layers and the user is painting on the 3rd layer, I need to draw a paint blob bitmap to the 3rd layer and then update the screen to show the result of flattening all layers on top of each other in real time. I've considered something along the lines of allocating my 6 bitmaps in C as int arrays and performing the painting operation on the Java side with Canvas using a copy of the layer being edited stored in a Bitmap object. I'm not sure how the flattening stage is going to work though.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Check out the "bitmap-plasma" sample in the NDK. It creates a bitmap in Java and manipulates the bits in native code. One possible technique is that you can allocate the large blocks of memory and hold your images in native code and simply render a "view" into a Java-created bitmap. The method to render the view and do the "flattening" of your image layers should probably be done in native code. Something along the lines of:

...user changed a layer...

My_native_render_code(MyDisplayBitmap);

invalidate();


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...