广告合作
  • 今日头条

    今日头条

  • 百度一下

    百度一下,你就知道

  • 新浪网

    新浪网 - 提供新闻线索,重大新闻爆料

  • 搜狐

    搜狐

  • 豆瓣

    豆瓣

  • 百度贴吧

    百度贴吧——全球领先的中文社区

  • 首页 尚未审核订阅工具 订阅

    android 纯c/c++开发

    来源:网络收集  点击:  时间:2024-02-25
    【导读】:
    android 自ndk r8出来以后,就开始支持纯c/c++开发,android 的纯 c/c++ 开发更有些想 win32 开发,只不过是 WinMain 变成了 android_main, 消息处理函数变成了两个,下面开始详细的介绍如何进行纯 c/c++开发,里面附带一个多点触屏的例子,希望对大家有用,谢谢! 代码视频的百度云链接:http://pan.baidu.com/s/1qWx3W1u 密码:i9rs 方便他人亦是方便自己,如果觉得还行就点下右边的投票吧,这样可以帮助其他人更快的找到解决问题的方法;有疑问的也可留言哦, 谢谢! 注:由于优酷对视频进行了压缩,特将高清版放到百度云,欢迎大家学习工具/原料morewin7 x64jdk1.8.0_11adt-bundle-windows-x86_64-20140702android-ndk-r10新建一个Natvie工程1/7分步阅读

    打开eclipse;

    2/7

    打开菜单-File-New-Android Application;

    3/7

    设置工程名,sdk版本,注意:主题设置为 None,点击next;

    4/7

    Configure Project 是取消 Create activity 的复选框,点击next;

    5/7

    Configure the attributes of the icon set, 直接点击 next;

    6/7

    Select whether to create an activity, and if so, what kind of activity. 点击 finish即可;

    7/7

    工程便创建出来了

    配置Makefile1/4

    右键工程NativeTest-弹出菜单-Android Tools-Add Native Support...

    2/4

    Settings for generated native components for project.界面 直接点击Finish

    3/4

    将 android.mk 的内容补充完整:

    LOCAL_PATH := $(call my-dir)

    include $(CLEAR_VARS)

    LOCAL_MODULE := NativeTest

    LOCAL_SRC_FILES := NativeTest.cpp

    LOCAL_LDLIBS := -llog -landroid

    LOCAL_STATIC_LIBRARIES := android_native_app_glue

    include $(BUILD_SHARED_LIBRARY)

    $(call import-module,android/native_app_glue)

    4/4

    增加一个Application.mk 文件(这一步可选),并写入:

    APP_ABI := x86

    APP_CPPFLAGS := --std=c++11

    NDK_TOOLCHAIN_VERSION := 4.8

    代码部分1/3

    1、android_main:这个函数类似于win32开发的WinMain函数

    2、app-onAppCmd = onAppCmd;

    app-onInputEvent = onInputEvent;

    类似于win32中设置窗口的回掉函数

    3、

    while ((ident=ALooper_pollAll(-1, NULL, events,

    (void**)source)) = 0) {

    // Process this event.

    if (source != NULL) {

    source-process(app, source);

    }

    // Check if we are exiting.

    if (app-destroyRequested != 0) {

    return;

    }

    }

    这一段类似于win32的消息循环

    4、为了方便大家粘贴,android_main 函数的代码如下:

    void android_main(struct android_app* app) {

    // Make sure glue isnt stripped.

    app_dummy();

    app-onAppCmd = onAppCmd;

    app-onInputEvent = onInputEvent;

    while (1) {

    int ident;

    int events;

    struct android_poll_source* source;

    while ((ident=ALooper_pollAll(-1, NULL, events,

    (void**)source)) = 0) {

    // Process this event.

    if (source != NULL) {

    source-process(app, source);

    }

    // Check if we are exiting.

    if (app-destroyRequested != 0) {

    return;

    }

    }

    }

    }

    2/3

    onAppCmd 描述的是真个activity的生命周期,类似于win32开发的消息处理回掉函数:

    static void onAppCmd(struct android_app* app, int32_t cmd) {

    switch (cmd) {

    case APP_CMD_SAVE_STATE:

    // The system has asked us to save our current state. Do so.

    __android_log_print(ANDROID_LOG_DEBUG, fuke, engine_handle_cmd APP_CMD_SAVE_STATE);

    break;

    case APP_CMD_INIT_WINDOW:

    // The window is being shown, get it ready.

    __android_log_print(ANDROID_LOG_DEBUG, fuke, engine_handle_cmd APP_CMD_INIT_WINDOW);

    break;

    case APP_CMD_TERM_WINDOW:

    __android_log_print(ANDROID_LOG_DEBUG, fuke, engine_handle_cmd APP_CMD_TERM_WINDOW);

    break;

    case APP_CMD_GAINED_FOCUS:

    // When our app gains focus, we start monitoring the accelerometer.

    __android_log_print(ANDROID_LOG_DEBUG, fuke, engine_handle_cmd APP_CMD_GAINED_FOCUS);

    break;

    case APP_CMD_LOST_FOCUS:

    // When our app loses focus, we stop monitoring the accelerometer.

    // This is to avoid consuming battery while not being used.

    __android_log_print(ANDROID_LOG_DEBUG, fuke, engine_handle_cmd APP_CMD_LOST_FOCUS);

    break;

    }

    }

    3/3

    onInputEvent 主要是用来触屏相关事件,也类似于win32开发的消息处理回掉函数,函数有两部分组成:

    1、检测多点触屏,并通过logcat打印出多点触屏的信息;

    2、控制屏幕颜色变化,每次松开手时颜色变化

    3、为方便大家粘贴,onInputEvent函数的代码记录如下:

    static int32_t onInputEvent(struct android_app* app, AInputEvent* event) {

    if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {

    int nNum = AMotionEvent_getPointerCount(event);

    char szTrace = {0};

    sprintf (szTrace, engine_handle_input num=, nNum);

    for (int nIdx = 0; nIdx nNum; nIdx++)

    {

    int nX = AMotionEvent_getX(event, 0);

    int nY = AMotionEvent_getY(event, 0);

    sprintf (strrchr(szTrace, 0), (%d %d), nX, nY);

    }

    __android_log_print(ANDROID_LOG_DEBUG, colorspace,

    %s, szTrace);

    if (AKeyEvent_getAction(event) != AKEY_EVENT_ACTION_UP)

    return 1;

    ANativeWindow_BuffernativeWindow = {0};

    int nRet = ANativeWindow_lock(app-pendingWindow, nativeWindow, NULL);

    int nArea = nativeWindow.width * nativeWindow.height;

    unsigned long*pdwScreen = (unsigned long*)nativeWindow.bits;

    static int s_nClr = 0;

    unsigned long pdwClr = {

    0x00000000, 0x000000ff, 0x0000ffff, 0x0000ff00,

    0x00ffff00, 0x00ff0000, 0x00ff00ff, 0x00ffffff};

    s_nClr ++;

    if (s_nClr sizeof(pdwClr) / sizeof(unsigned long))

    s_nClr = 0;

    for (int nIdx = 0; nIdx nArea; nIdx++)

    {

    pdwScreen = pdwClr;

    }

    ANativeWindow_unlockAndPost(app-pendingWindow);

    return 1;

    }

    return 0;

    }

    设置工程属性1/7

    1、打开AndroidManifest.xml

    2、打开 Application 分页

    3、增加一个 Activity

    如下所示:

    2/7

    1、选择右边的 Browse;

    2、取消 Display classes from sources of ... 前面的复选框;

    3、在搜索栏输入na,选中列出来的 NativeActivity

    4、点击OK

    效果如下:

    3/7

    1、选中 android.app.nativeActivity

    2、点击 add

    3、选择 Meta Data

    4、点击Ok

    4/7

    输入:

    android:name=android.app.lib_name

    android:value=NativeTest

    5/7

    1、选中 android.app.nativeActivity

    2、点击 add

    3、选择 Intent Filter

    4、点击Ok

    6/7

    1、选中 Intent Filter

    2、点击 add

    3、选择 Action

    4、点击Ok

    5、设置android:name=android.intent.action.MAIN

    7/7

    1、选中Intent Filter

    2、点击 add

    3、选择 Category

    4、点击Ok

    5、设置android:name=android.intent.category.LAUNCHER

    运行1/3

    启动模拟器,运行效果如下:

    2/3

    点击后效果:

    3/3

    这次整个程序完成

    总结1/2

    整个程序实现:

    1、android 下面的纯c/c++ 开发

    2、实现了多点触屏的功能

    3、实现了点击屏幕颜色的切换功能

    2/2

    方便他人亦是方便自己,如果觉得还行就点下下边的投票吧,这样可以帮助其他人更快的找到解决问题的方法;有疑问的也可留言哦, 谢谢!

    android
    本文关键词:

    版权声明:

    1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。

    2、本站仅提供信息发布平台,不承担相关法律责任。

    3、若侵犯您的版权或隐私,请联系本站管理员删除。

    4、文章链接:http://www.1haoku.cn/art_150405.html

    相关资讯

    ©2019-2020 http://www.1haoku.cn/ 国ICP备20009186号05-06 20:39:40  耗时:0.027
    0.0275s