Androidネイティブレイヤーは、JAVAレイヤーメソッドを呼び出して、テキストテクスチャを持つメソッドを生成します

多くの場合、openglesを使用してネイティブレイヤーでレンダリングおよび描画したい場合は、テキストをレンダリングして描画したいと考えています。
しかし、OpenGL ESは実際にはテキストの直接描画をサポートしていませんが、Android JAVAレイヤーにはビットマップの使用などのテキスト描画メソッドの完全なセットがあり、ビットマップにテキストを描画するためのコードは次のとおりです。

//create the canvas
Bitmap originBmp = Bitmap.createBitmap(320, 200, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(originBmp);

//get content from res
textPaint.setTextSize(15.0F);
String content = act.getString(R.string.dialog_content);
StaticLayout clayout= new StaticLayout(content , textPaint, originBmp.getWidth()-8,
        Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
canvas.translate(0, 40);
clayout.draw(canvas);

このビットマップとテキストをNativeに渡し、テクスチャにバインドして、openglesレンダリングテキストを実現します
。実際、openglesは、ビットマップをテクスチャに直接バインドできる一連のJavaレイヤーAPIも提供しています。コードは次のとおりです。

//this static method must be called by opengl rendor thread
//which opengles environment has been successfully setup.
public static int generateSeethroughDilogTexture(Activity act,String text) {
    
    
   Log.e(TAG,"generateSeethroughDilogTexture "+text+" E");
   //create the canvas
   Bitmap originBmp = Bitmap.createBitmap(320, 200, Bitmap.Config.ARGB_8888);
   Canvas canvas = new Canvas(originBmp);

   //get dialog content from res
   textPaint.setTextSize(15.0F);
   String content = act.getString(R.string.dialog_content);
   text = content;
   StaticLayout clayout= new StaticLayout(text, textPaint, originBmp.getWidth()-8,
           Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
   canvas.translate(0, 40);
   clayout.draw(canvas);

   int[] tex = new int[1];//need delete later
   GLES20.glGenTextures(1, tex, 0);
   GLES20.glBindTexture(GL10.GL_TEXTURE_2D, tex[0]);
   GLES20.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
   GLES20.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
   GLES20.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
   GLES20.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
   GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, originBmp , 0);
   textPaint=null;
   canvas   =null;
   newBitmap.recycle();
   newBitmap=null;
   Log.d(TAG,"generateSeethroughDilogTexture texId:"+ tex[0]+" X");
   return tex[0];
}

ネイティブレイヤーにtexを渡すだけで済みます
。Javaコードは、Nativeメソッドを通じてネイティブレイヤーにテクスチャIDをアクティブに設定できます。このメソッドの多くのデモがあります。これは導入されていません。
ネイティブは、Javaメソッドをアクティブに呼び出して、テクスチャID
コードを取得することもできます。次のように:

int generateSeethroughDilogTexture(TimeWarpInitParms* initParms)
{
    
    
    LOG( "generateSeethroughDilogTexture E");
    // The current thread is presumably already attached, so this
    // should just return a valid environment.
    JNIEnv * jniEnv=NULL;
    const jint rtn = JavaVm->AttachCurrentThread(&jniEnv, 0);
    if (rtn != JNI_OK) {
    
    
        LOG( "AttachCurrentThread() returned %i", rtn);
    }

    //in unity app,this activity is UnityPlayerNativeActivity.java
    jclass activityClass = jniEnv->GetObjectClass(initParms->ActivityObject);
    if (activityClass == NULL){
    
    
        LOG("try activityClass failed!");
        return -1;
    }
	const jmethodID method = jniEnv->GetStaticMethodID( activityClass,
            "generateSeethroughDilogTexture",
            "(Landroid/app/Activity;Ljava/lang/String;)I"  );
	if ( !method ){
    
    
        LOG("try method failed");
        return -1;
	}
	const char* content = "this is my test";

    jstring jcontent = jniEnv->NewStringUTF(content);//may be shou delete later

    int textureId = jniEnv->CallStaticIntMethod( activityClass, method,initParms->ActivityObject,jcontent);
    LOG( "generateSeethroughDilogTexture textureId %d X",textureId);
    JavaVm->DetachCurrentThread();
    return textureId;
}

おすすめ

転載: blog.csdn.net/u010116586/article/details/103569506