NDK 简介

AndroidSDK 提供了一套OpenGLES 接口,该接口是基于 Java 的,速度非常 慢,往往很难满足需要。

NDK 提供了一系列的工具,帮助开发者快速开发C(或C++)的动态库,并能自动 将 so和 java 应用一起打包成 apk。使用 NDK,我们可以将要求高性能的应用 逻辑使用C开发,从而提高应用程序的执行效率。如OpenGLES 的程序。2

JNI

对于 NDK,在 Java 代码中调用 C/C++ 代码是通过 JNI 实现的。

Java Native Interface(JNI)是 Java 语言的本地编程接口,在java程序中, 我们可以通过 JNI 实现一些用 java 语言不便实现的功能。通常有以下几种情 况我们需要使用 JNI 来实现:

  1. java 类库没有提供你的应用程序所需要的功能,通常这些功能是平台相关的;
  2. 你希望使用一些已经有的类库或者应用程序,而他们并非用java语言编写的;
  3. 程序的某些部分对速度要求比较苛刻,你选择用汇编或者 c 语言来实现并在 java 语言中调用他们。

注意,不到万不得已不要使用 JNI 技术,一方面它需要你把握更多的知识才可 以驾驭,一方面使用了 JNI 你的程序就会丧失可移植性。

我们可以把 JNI 想象为本地代码和 java 代码的粘合剂,关系如下图所示3:

OpenGLES

Java 端核心代码如下所示:
public class DemoAct extends Activity {    private GLSurfaceView mGLView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        mGLView = new DemoGLSurfaceView(this);        setContentView(mGLView);    }    ... ...    static {        System.loadLibrary("sanangeles");    }}class DemoGLSurfaceView extends GLSurfaceView {    DemoRenderer mRenderer;    public DemoGLSurfaceView(Context context) {        super(context);        mRenderer = new DemoRenderer();        setRenderer(mRenderer);    }class DemoRenderer implements GLSurfaceView.Renderer {    private static native void nativeInit();    private static native void nativeResize(int w, int h);    private static native void nativeRender();    private static native void nativeDone();    public void onSurfaceCreated(GL10 gl, EGLConfig config) {        nativeInit();    }    public void onSurfaceChanged(GL10 gl, int w, int h) {        nativeResize(w, h);    }    public void onDrawFrame(GL10 gl) {        nativeRender();    }}

注意:EGL Context Lost

There are situations where the EGL rendering context will be lost. This typically happens when device wakes up after going to sleep. When the EGL context is lost, allOpenGLresources (such as textures) that are associated with that context will be automatically deleted. In order to keep rendering correctly, a renderer must recreate any lost resources that it still needs. The onSurfaceCreated(GL10, EGLConfig) method is a convenient place to do this.

JNI 代码如下所示:
#include <jni.h>.../* Call to initialize the graphics state */voidJava_com_example_AndMii_DemoRenderer_nativeInit( JNIEnv*  env ){    appInit();}/* Call to finalize the graphics state */voidJava_com_example_AndMii_DemoRenderer_nativeDone( JNIEnv*  env ){    free objects.}/* Call to render the next GL frame */voidJava_com_example_AndMii_DemoRenderer_nativeRender( JNIEnv*  env ){    curTime = ...    appRender(curTime, sWindowWidth, sWindowHeight);}

C 语言端
void appInit(){    glEnable(GL_NORMALIZE);    glEnable(GL_DEPTH_TEST);    glDisable(GL_CULL_FACE);    glShadeModel(GL_FLAT);    glEnable(GL_LIGHTING);    glEnable(GL_LIGHT0);    glEnable(GL_LIGHT1);    glEnable(GL_LIGHT2);    glEnableClientState(GL_VERTEX_ARRAY);    glEnableClientState(GL_COLOR_ARRAY);}// Called from the app framework./* The tick is current time in milliseconds, width and height * are the image dimensions to be rendered. */void appRender(long tick, int width, int height){    // Prepare OpenGL ES for rendering of the frame.    prepareFrame(width, height);    // Configure environment.    configureLightAndMaterial();    // Draw all the models normally.    drawModels(1);}static void prepareFrame(int width, int height){    glViewport(0, 0, width, height);    glClearColorx((GLfixed)(0.1f * 65536),                  (GLfixed)(0.2f * 65536),                  (GLfixed)(0.3f * 65536), 0x10000);    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    gluPerspective(45, (float)width / height, 0.5f, 150);    glMatrixMode(GL_MODELVIEW);    glLoadIdentity();}static void configureLightAndMaterial(){    static GLfixed light0Position[] = { -0x40000, 0x10000, 0x10000, 0 };    static GLfixed light0Diffuse[] = { 0x10000, 0x6666, 0, 0x10000 };    static GLfixed light1Position[] = { 0x10000, -0x20000, -0x10000, 0 };    static GLfixed light1Diffuse[] = { 0x11eb, 0x23d7, 0x5999, 0x10000 };    static GLfixed light2Position[] = { -0x10000, 0, -0x40000, 0 };    static GLfixed light2Diffuse[] = { 0x11eb, 0x2b85, 0x23d7, 0x10000 };    static GLfixed materialSpecular[] = { 0x10000, 0x10000, 0x10000, 0x10000 };    glLightxv(GL_LIGHT0, GL_POSITION, light0Position);    glLightxv(GL_LIGHT0, GL_DIFFUSE, light0Diffuse);    glLightxv(GL_LIGHT1, GL_POSITION, light1Position);    glLightxv(GL_LIGHT1, GL_DIFFUSE, light1Diffuse);    glLightxv(GL_LIGHT2, GL_POSITION, light2Position);    glLightxv(GL_LIGHT2, GL_DIFFUSE, light2Diffuse);    glMaterialxv(GL_FRONT_AND_BACK, GL_SPECULAR, materialSpecular);    glMaterialx(GL_FRONT_AND_BACK, GL_SHININESS, 60 << 16);    glEnable(GL_COLOR_MATERIAL);}static void drawModels(float zScale){    glVertexPointer(......);    glColorPointer(......);    if (normalArray != NULL)    {        glNormalPointer(GL_FIXED, 0, normalArray);        glEnableClientState(GL_NORMAL_ARRAY);    }    else        glDisableClientState(GL_NORMAL_ARRAY);    glDrawArrays(GL_TRIANGLES, 0, object->count);}

调试

要想在 jni native 代码中看打印信息,printf 是不行的,需使用 __android_log_print,如下所示。

__android_log_print(ANDROID_LOG_INFO, "ProjectName", "I am : %d\n", n);

该函数与 printf 用法相似,使用格式字符。打印的结果通过 logcat 查看。

注意,使用时需要将头文件 android/log.h 包含进来。

为方便使用,往往定义一些宏

#include <android/log.h>#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, "ProjectName", __VA_ARGS__)#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG  , "ProjectName", __VA_ARGS__)#define LOGI(...) __android_log_print(ANDROID_LOG_INFO   , "ProjectName", __VA_ARGS__)#define LOGW(...) __android_log_print(ANDROID_LOG_WARN   , "ProjectName", __VA_ARGS__)#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR  , "ProjectName", __VA_ARGS__)

转帖: http://www.linuxgraphics.cn/android/ndk_opengl.html

更多相关文章

  1. 使用Android辅助服务AccessibilityService实现的微信自动抢红包
  2. H5唤起android app,启动关联应用
  3. Android(安卓)studio下的Android(安卓)JNI调用以及动态链接库.so
  4. Android平台上最好的几款免费代码编辑器
  5. 也谈Android下一个apk安装多个程序入口图标
  6. MDC Android专场:账户同步备份框架与Web&Native混合开发
  7. Android学习系列(5)--App布局初探之简单模型
  8. Android:“万能”Activity重构篇
  9. android程序自动化生成apk的过程

随机推荐

  1. android反编译apk文件
  2. IDA动态调试Android的DEX文件
  3. android后台获取当前屏幕截图(screencap.c
  4. Eclipse 不能finish android工程
  5. 2012-7-23 android shape
  6. SharePreferences实现
  7. android 学习之---Android之GoogleMap(转
  8. Android home键和back键区别
  9. Android中使用SVG矢量图打造多边形图形框
  10. android重写finish实现是否退出提示功能