Android NDK-如何创建多个只给定大小(宽度和高度)的位图

Android NDK - how to create multiple bitmaps given only size (width and height)

本文关键字:位图 高度 何创建 NDK- 创建 Android      更新时间:2023-10-16

我是这方面的新手——提问、android开发和NDK。我希望我足够清楚。

我需要能够创建多个曲面/位图。例如

Surface s = new Surface (width, height)
  • 他们可以相互复制s->复制(s2)将曲面s复制到s2(包括RGBA和alpha文本曲面之间的格式转换以及调整大小/缩放)
  • 使用fill(x,u,w,h,color)-用颜色填充矩形(类似glClear)

据我所知,你只有一个ANativeWindow,它是由android_app->window变量提供给你的,如果我使用EGL,我可以创建多达1个EGLSurface。我需要能够创建许多曲面(例如大约100个)。这怎么可能?然后将它们全部闪电式传输到窗口帧缓冲区

还有安卓/位图。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)、尺寸、步幅或音高的信息。

CCD_ 4给出了原始像素。完成像素操作后,必须调用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 ();

您可以创建位图并使用jnigraphics库(android/bitmap.h),也可以使用多个EGL纹理。

使用位图,您必须自己实现fill,因为位图只有基于像素的getter和setter(请参见setPixels(..)