您可以通过 JNI 调用创建位图:
// setup bitmap class
jclass bitmap_class = (jclass)env->FindClass ("android/graphics/Bitmap");
// setup create method
jmethodID bitmap_create_method = env->GetStaticMethodID (bitmap_class, "createBitmap", "(IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;");
// get_enum_value return jobject corresponding in our case to Bitmap.Config.ARGB_8888. (the implentation is irrelevant here)
jobject bitmap_config_ARGB = get_enum_value ("android/graphics/Bitmap$Config", "ARGB_8888");
// Do not forget to call DeleteLocalRef where appropriate
// create the bitmap by calling the CreateBitmap method
// Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
jobject bitmap = env->CallStaticObjectMethod (bitmap_class, bitmap_create_method, width, height, bconfig);
// at the end of course clean-up must be done
env->DeleteLocalRef (bitmap);
您可以通过 API 访问一些位图属性和原始像素android/bitmap.h
AndroidBitmap_getInfo
提供有关格式(ARGB_8888
或仅 alpha)、尺寸、步幅或间距的信息。
AndroidBitmap_lockPixels
给出原始像素。完成对像素的操作后,必须调用AndroidBitmap_unlockPixels
做fill (color, dimension)
JNI 可以提供帮助。这可以通过 JNI 调用来编写(我将使用 java,因为它更容易编写并且更易于阅读)。
canvas.save ();
canvas.setBitmap (bitmap);
canvas.clipRect (left, top, right, bottom, Region.Op.REPLACE);
canvas.drawColor (color, PorterDuff.Mode.SRC);
canvas.restore ();
将一个位图复制到另一个位图 -copy (src_bitmap, src_rect, dest_rect)
canvas.save ();
canvas.setBitmap (dest_bitmap);
canvas.clipRect (left, top, right, bottom, Region.Op.REPLACE);
canvas.drawBitmap (src_bitmap, src_rect, dest_rect, null);
canvas.restore ();