织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>嵌入式开发>Android>

无Java开发Android应用(NativeActivity)

落鹤生 发布于 2011-01-19 18:04 点击:次 
最新的 Android 2.3 无需 Java 就可以开发应用,详情请看 这里。这里是官方给的例子程序 ,来自:http://developer.android.com/reference/android/app/NativeActivity.html
TAG:

最新的 Android 2.3 无需 Java 就可以开发应用,详情请看 这里
这里是官方给的例子程序 ,来自:http://developer.android.com/reference/android/app/NativeActivity.html

[代码] AndroidManifest.xml

  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  2.         package="com.example.native_activity" 
  3.         android:versionCode="1" 
  4.         android:versionName="1.0"> 
  5.  
  6.     <!-- This is the platform API where NativeActivity was introduced. --> 
  7.     <uses-sdk android:minSdkVersion="8" /> 
  8.  
  9.     <!-- This .apk has no Java code itself, so set hasCode to false. --> 
  10.     <application android:label="@string/app_name" android:hasCode="false"> 
  11.  
  12.         <!-- Our activity is the built-in NativeActivity framework class. 
  13.              This will take care of integrating with our NDK code. --> 
  14.         <activity android:name="android.app.NativeActivity" 
  15.                 android:label="@string/app_name" 
  16.                 android:configChanges="orientation|keyboardHidden"> 
  17.             <!-- Tell NativeActivity the name of or .so --> 
  18.             <meta-data android:name="android.app.lib_name" 
  19.                     android:value="native-activity" /> 
  20.             <intent-filter> 
  21.                 <action android:name="android.intent.action.MAIN" /> 
  22.                 <category android:name="android.intent.category.LAUNCHER" /> 
  23.             </intent-filter> 
  24.         </activity> 
  25.     </application> 
  26.  
  27. </manifest>  

[代码] Demo.c

  1. #include <jni.h> 
  2. #include <errno.h> 
  3.  
  4. #include <EGL/egl.h> 
  5. #include <GLES/gl.h> 
  6.  
  7. #include <android/sensor.h> 
  8. #include <android/log.h> 
  9. #include <android_native_app_glue.h> 
  10.  
  11. #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO
  12. , "native-activity", __VA_ARGS__)) 
  13. #define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN
  14. , "native-activity", __VA_ARGS__)) 
  15.  
  16. /** 
  17.  * Our saved state data. 
  18.  */ 
  19. struct saved_state { 
  20.     float angle; 
  21.     int32_t x; 
  22.     int32_t y; 
  23. }; 
  24.  
  25. /** 
  26.  * Shared state for our app. 
  27.  */ 
  28. struct engine { 
  29.     struct android_app* app; 
  30.  
  31.     ASensorManager* sensorManager; 
  32.     const ASensor* accelerometerSensor; 
  33.     ASensorEventQueue* sensorEventQueue; 
  34.  
  35.     int animating; 
  36.     EGLDisplay display; 
  37.     EGLSurface surface; 
  38.     EGLContext context; 
  39.     int32_t width; 
  40.     int32_t height; 
  41.     struct saved_state state; 
  42. }; 
  43.  
  44. /** 
  45.  * Initialize an EGL context for the current display. 
  46.  */ 
  47. static int engine_init_display(struct engine* engine) { 
  48.     // initialize OpenGL ES and EGL 
  49.  
  50.     /* 
  51.      * Here specify the attributes of the desired configuration. 
  52.      * Below, we select an EGLConfig with at least 8 bits per color 
  53.      * component compatible with on-screen windows 
  54.      */ 
  55.     const EGLint attribs[] = { 
  56.             EGL_SURFACE_TYPE, EGL_WINDOW_BIT, 
  57.             EGL_BLUE_SIZE, 8, 
  58.             EGL_GREEN_SIZE, 8, 
  59.             EGL_RED_SIZE, 8, 
  60.             EGL_NONE 
  61.     }; 
  62.     EGLint w, h, dummy, format; 
  63.     EGLint numConfigs; 
  64.     EGLConfig config; 
  65.     EGLSurface surface; 
  66.     EGLContext context; 
  67.  
  68.     EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); 
  69.  
  70.     eglInitialize(display, 0, 0); 
  71.  
  72.     /* Here, the application chooses the configuration it desires. In this 
  73.      * sample, we have a very simplified selection process, where we pick 
  74.      * the first EGLConfig that matches our criteria */ 
  75.     eglChooseConfig(display, attribs, &config, 1, &numConfigs); 
  76.  
  77.     /* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is 
  78.      * guaranteed to be accepted by ANativeWindow_setBuffersGeometry(). 
  79.      * As soon as we picked a EGLConfig, we can safely reconfigure the 
  80.      * ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */ 
  81.     eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format); 
  82.  
  83.     ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format); 
  84.  
  85.     surface = eglCreateWindowSurface(display, config, engine->app->window, NULL); 
  86.     context = eglCreateContext(display, config, NULL, NULL); 
  87.  
  88.     if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) { 
  89.         LOGW("Unable to eglMakeCurrent"); 
  90.         return -1; 
  91.     } 
  92.  
  93.     eglQuerySurface(display, surface, EGL_WIDTH, &w); 
  94.     eglQuerySurface(display, surface, EGL_HEIGHT, &h); 
  95.  
  96.     engine->display = display; 
  97.     engine->context = context; 
  98.     engine->surface = surface; 
  99.     engine->width = w; 
  100.     engine->height = h; 
  101.     engine->state.angle = 0; 
  102.  
  103.     // Initialize GL state. 
  104.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); 
  105.     glEnable(GL_CULL_FACE); 
  106.     glShadeModel(GL_SMOOTH); 
  107.     glDisable(GL_DEPTH_TEST); 
  108.  
  109.     return 0; 
  110.  
  111. /** 
  112.  * Just the current frame in the display. 
  113.  */ 
  114. static void engine_draw_frame(struct engine* engine) { 
  115.     if (engine->display == NULL) { 
  116.         // No display. 
  117.         return
  118.     } 
  119.  
  120.     // Just fill the screen with a color. 
  121.     glClearColor(((float)engine->state.x)/engine->width, engine->state.angle, 
  122.             ((float)engine->state.y)/engine->height, 1); 
  123.     glClear(GL_COLOR_BUFFER_BIT); 
  124.  
  125.     eglSwapBuffers(engine->display, engine->surface); 
  126.  
  127. /** 
  128.  * Tear down the EGL context currently associated with the display. 
  129.  */ 
  130. static void engine_term_display(struct engine* engine) { 
  131.     if (engine->display != EGL_NO_DISPLAY) { 
  132.         eglMakeCurrent(engine->display, EGL_NO_SURFACE, EGL_NO_SURFACE
  133. , EGL_NO_CONTEXT); 
  134.         if (engine->context != EGL_NO_CONTEXT) { 
  135.             eglDestroyContext(engine->display, engine->context); 
  136.         } 
  137.         if (engine->surface != EGL_NO_SURFACE) { 
  138.             eglDestroySurface(engine->display, engine->surface); 
  139.         } 
  140.         eglTerminate(engine->display); 
  141.     } 
  142.     engine->animating = 0; 
  143.     engine->display = EGL_NO_DISPLAY; 
  144.     engine->context = EGL_NO_CONTEXT; 
  145.     engine->surface = EGL_NO_SURFACE; 
  146.  
  147. /** 
  148.  * Process the next input event. 
  149.  */ 
  150. static int32_t engine_handle_input(struct android_app* app, AInputEvent* event) { 
  151.     struct engine* engine = (struct engine*)app->userData; 
  152.     if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) { 
  153.         engine->animating = 1; 
  154.         engine->state.x = AMotionEvent_getX(event, 0); 
  155.         engine->state.y = AMotionEvent_getY(event, 0); 
  156.         return 1; 
  157.     } 
  158.     return 0; 
  159.  
  160. /** 
  161.  * Process the next main command. 
  162.  */  (红薯 )
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201101/10785.html]
本文出处:开源中国社区 作者:红薯
顶一下
(1)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容