Android
Android Drawing on Android: Canvas vs. OpenGL Bitmap vs. Texture NDK: Native Development Kit
Canvas Canvas.drawLine(), drawRect(), etc. Canvas.setMatrix(m); Canvas.setBitmap(bmp); View.onDraw(Canvas);
Bitmap Bitmap.createBitmap(width, height, config); BitmapFactory.decodeFile(file, options); With or without alpha channel (transparency).
View: events & invalidation onDraw(canvas) invalidate() → onDraw() onTouchEvent() → invalidate()
PixelFormat RGB_565 (2 bytes / pixel) ARGB_8888 (4 bytes / pixel) Bitmap.Config getWindow().setFormat() PixelFormat.RGBA_8888
Dithering
Canvas Not hardware accelerated (CPU) Uses Bitmap for images Rendering in UI thread, invalidate() model Fast scrolling Slow zooming, rotation, 3d projections
OpenGL ES Hardware accelerated (GPU) Uses Textures for images Rendering in separate thread (not UI thread) Fast zooming, 3d projections Fragmentation, complexity, hard to debug
Bitmap vs. Texture Bitmaps: use Java heap, limited to 24MB. 4bytes/pixel (ARGB) Textures: use native memory, limited only by total RAM. can use 3bytes/pixel (RGB). support pixmaps. may use fast GPU memory.
Native Development Kit (NDK)
Native Development Kit (NDK) JNI: Java Native Interface C, C++ GCC 4.4.3 cross compiler STL, exceptions, RTTI arm-linux-gnueabi, RISC, little endian, 32bit Thumb or ARM instructions Without hardware floating point
Why use NDK Performance critical code, e.g. FFT, game physics library, image/sound processing. Performance: avoid Java object creation and GC, e.g. filesystem scan. Use existing non-Java code/libraries (e.g. Python interpreter in C).
Java Native Interface (JNI) class Hello { public native static int sum(int[] data); static { System.loadLibrary(“hello”); }
JNI C++ code JNI book
Stable APIs C library Math library Zlib OpenGL ES 1.x & 2.0 OpenSL ES Android Log Bitmap interface (jnigraphics) Android native application API
NDK and Market Native code compiled for multiple architectures in a single APK. Market does filtering based on architectures available in APK and on platform version.
Thank you