我是新手 - 问问题、android 开发和 NDK。我希望我已经足够清楚了。
我需要能够创建多个表面/位图。 例如
Surface s = new Surface (width, height)
- 他们可以互相复制 s->copy (s2) 将表面s复制到s2(包括RGBA和alpha-text表面之间的格式转换和调整大小/缩放)
- 使用 fill (x, u, w, h, color) - 用颜色填充矩形(类似于 glClear)
据我所知,您只有一个 ANativeWindow,它由 android_app->window 变量提供给您,如果我使用 EGL,我最多可以创建 1 个 EGLSurface。我需要能够创建许多表面(例如约 100 个)。这怎么可能?然后将它们全部blit到窗口帧缓冲区
还有 android/bitmap.h 但我不明白如何使用它。但它不提供 API 来创建表面,只是为了已经创建或类似的东西?
最佳答案
您可以通过 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);
您可以通过 android/bitmap.h
中的 API 访问一些位图属性和原始像素
AndroidBitmap_getInfo
提供有关格式(ARGB_8888
或仅 alpha)、尺寸、步幅或间距的信息。
AndroidBitmap_lockPixels
给出原始像素。完成像素操作后必须调用 AndroidBitmap_unlockPixels
制作填充(颜色,尺寸)
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 ();
关于Android NDK - 如何创建仅给定大小(宽度和高度)的多个位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11778803/