如何用c++写安卓手机软件?

最好有具体的操作步骤,语句可以自己写。

用c++写安卓手机软件的方法:

安装ndk,使用纯c++开发安卓程序,下边是详细的步骤与说明:

1、编写入口函数

android_main为入口函数,和C++中的main函数是一样的。这里创建CELLAndroidApp的对象,直接调用main函数。

void android_main(struct android_app* state)  

{  

    CELLAndroidApp    app(state);  

    app.main(0,0);  

}  

2.绘制类的实现说明

   protected:  

    EGLConfig        _config;  

    EGLSurface       _surface;  

    EGLContext       _context;  

    EGLDisplay       _display;  

    android_app*     _app;  

    int              _width;  

    int              _height; 

部分参数说明:

_surface:用于绘制图形,相当于windows绘图中的位图

_context:可以看做是opengl对象

_display:用于绘图的设备上下文,类似于windows绘图中的dc

3.构造函数说明

CELLAndroidApp(android_app* app):_app(app)      {          _surface    =    0;          _context    =    0;           _display    =    0;          _width        =    64;          _height        =    48;          app->userData        =    this; //用户数据          app->onAppCmd         =     handle_cmd; //窗口的创建销毁等          app->onInputEvent     =    handle_input; //回调函数      } 

值得注意的是,这里的app中的userData,传入用户数据,这里直接传入this,onAppCmd传入的handle_cmd回调函数,onInputEvent传入的事handle_input回调函数

4.类中函数main()说明

virtual    void     main(int argc,char** argv)  

    {  

        int ident;  

        int    events;  

        android_poll_source* source;  

 

        while (true)  

        {          

            while ((ident = ALooper_pollAll(0, NULL, &events, (void**)&source)) >= 0)   

            {  

                if (source != NULL)  

                    source->process(_app, source); //有触摸事件,调用input函数,相当于dispatchmessage  

 

                if (_app->destroyRequested != 0)  

                    return;  

            }  

            render();  

        }  

    } 

5.调用render()函数,绘制图形。

6.初始化设备函数initDevice()

virtual void     initDevice()  

    {  

        const EGLint attribs[] =  

        {  

            EGL_SURFACE_TYPE, EGL_WINDOW_BIT,  

            EGL_BLUE_SIZE, 8,   

            EGL_GREEN_SIZE, 8,  

            EGL_RED_SIZE, 8,  

            EGL_NONE  

        };  

        EGLint     format;  

        EGLint    numConfigs;  

 

        _display    =    eglGetDisplay(EGL_DEFAULT_DISPLAY);  

 

        eglInitialize(_display, 0, 0);  

 

        eglChooseConfig(_display, attribs, &_config, 1, &numConfigs);   

 

        eglGetConfigAttrib(_display, _config, EGL_NATIVE_VISUAL_ID, &format);  

 

        ANativeWindow_setBuffersGeometry(_app->window, 0, 0, format);   

 

        _surface    =     eglCreateWindowSurface(_display, _config, _app->window, NULL);  

 

#if 0  

        EGLint contextAtt[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };  

 

        _context     =     eglCreateContext(_display, _config, 0, contextAtt);  

#else  

        _context     =     eglCreateContext(_display, _config, 0, 0);   

#endif 

        if (eglMakeCurrent(_display, _surface, _surface, _context) == EGL_FALSE)  

        {  

            LOGW("Unable to eglMakeCurrent");   

            return;  

        }  

        eglQuerySurface(_display, _surface, EGL_WIDTH, &_width);   

        eglQuerySurface(_display, _surface, EGL_HEIGHT, &_height);  

        onCreate();  

        // Initialize GL state.  

        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);   

        glEnable(GL_CULL_FACE);  

        glShadeModel(GL_SMOOTH);  

        glDisable(GL_DEPTH_TEST);  

        glViewport(0,0,_width,_height);  

        glOrthof(0,_width,_height,0,-100,100);  

7.绘制函数render()

virtual    void     render()  

    {  

        if(_display == 0)  

        {  

            return;  

        }  

        glClearColor(0,0,0, 1);   

        glClear(GL_COLOR_BUFFER_BIT);  

 

        glEnableClientState(GL_VERTEX_ARRAY);   

        if(g_arVertex.size() >= 2)  

        {  

            glColor4f(1,1,1,1);   

            glVertexPointer(3,GL_FLOAT,0,&g_arVertex[0]);   

            glDrawArrays(GL_LINE_STRIP,0,g_arVertex.size());  

        }  

 

        eglSwapBuffers(_display,_surface); //双缓存的交换缓冲区  

    } 

8.编译程序,将程序导入到模拟器中,最终运行的效果图如下:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-09-03
这个你不容易,安卓是java写的,你学过C和C++想写安卓软件既需要学java语言,又需要学安卓手机的接口,相当于新学,不天天学两三年写不出什么东西的。建议写个win7能用的加壳程序,现在的壳基本上是以前系统上的,win7实用的不多。本回答被网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网