La capa nativa de Android llama al método de la capa JAVA para generar un método con textura de texto

Muchas veces, si queremos usar opengles para renderizar y dibujar en la capa nativa, esperamos renderizar y dibujar algo de texto.
Pero OpenGL ES en realidad no admite el dibujo de texto directamente, pero la capa JAVA de Android tiene un conjunto completo de métodos de dibujo de texto, como usar mapa de bits, el código para dibujar texto en mapa de bits es el siguiente:

//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);

Pase este mapa de bits con texto a Native y luego vincúlelo a la textura para lograr que opengles renderice el texto
. De hecho, opengles también proporciona un conjunto de API de capa Java que pueden vincular directamente el mapa de bits a la textura. El código es el siguiente:

//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];
}

Solo necesita pasar el texto a la capa nativa. El
código java puede establecer activamente la identificación de la textura en la capa nativa a través del método nativo. Hay muchas demostraciones de este método. No se presenta.
Native también puede llamar activamente al método Java para obtener el
código de identificación de la textura . como sigue:

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;
}

Supongo que te gusta

Origin blog.csdn.net/u010116586/article/details/103569506
Recomendado
Clasificación