GLSurfaceView渲染RGB的数据,利用双缓冲空间机制。取出缓冲地址,将要渲染的数据copy到缓冲地址.
获取Surface
Java部分:
SurfaceView控件设置到界面中显示
SurfaceView定义一个类来实现
package com.example.hello;import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.view.SurfaceHolder;import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;public class Xpaly extends GLSurfaceView implements Runnable, SurfaceHolder.Callback {public Xpaly(Context context , AttributeSet attrs) {super(context);}@Overridepublic void run() {Open("/sdcard/Pictures/Rc.mp4",getHolder().getSurface());}@Overridepublic void surfaceCreated(SurfaceHolder var1){}@Overridepublic void surfaceChanged(SurfaceHolder var1, int var2, int var3, int var4){new Thread(this).start();}@Overridepublic void surfaceDestroyed(SurfaceHolder var1){}public native void Open(String url_,Object surface);}
实现SurfaceView Callback
需要android的库和头文件
#include <android/native_window.h>
#include <android/native_window_jni.h>
C++ 部分
//显示窗口初始化ANativeWindow *nwin = ANativeWindow_fromSurface(env,surface);int outWidth=vc->width;int outHeight=vc->height;//设置native window的buffer大小,可自动拉伸ANativeWindow_setBuffersGeometry(nwin,outWidth,outHeight,WINDOW_FORMAT_RGBA_8888);ANativeWindow_Buffer wbuf;//锁定窗口,取出空间ANativeWindow_lock(nwin,&wbuf,0);//取缓冲区地址uint8_t *dst = (uint8_t*)wbuf.bits;//数据拷贝到缓冲区中memcpy(dst,rgb,outWidth*outHeight*4);//解锁窗口空间ANativeWindow_unlockAndPost(nwin);