2021-05-12 14:32:11
android 純c/c++開發
android 自ndk r8出來以後,就開始支援純c/c++開發,android 的純 c/c++ 開發更有些想 win32 開發,只不過是 WinMain 變成了 android_main, 訊息處理常式變成了兩個,下面開始詳細的介紹如何進行純 c/c++開發,裡面附帶一個多點觸控螢幕的例子,希望對大家有用,謝謝!
程式碼視訊的百度雲連結:http://pan.baidu.com/s/1qWx3W1u 密碼:i9rs
方便他人亦是方便自己,如果覺得還行就點下右邊的投票吧,這樣可以幫助其他人更快的找到解決問題的方法;有疑問的也可留言哦, 謝謝!
註:由於優酷對視訊進行了壓縮,特將高清版放到百度雲,歡迎大家學習
1
開啟eclipse;2
開啟選單->File->New->Android Application;
3
設定工程名,sdk版本,注意:主題設定為 None,點選next;
4
Configure Project 是取消 Create activity 的核取方塊,點選next;
5
Configure the attributes of the icon set, 直接點選 next;
6
Select whether to create an activity, and if so, what kind of activity. 點選 finish即可;
7
工程便建立出來了
1
右鍵工程NativeTest->彈出選單->Android Tools->Add Native Support...
2
Settings for generated native components for project.介面 直接點選Finish
3
將 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
增加一個 Application.mk 檔案(這一步可選),並寫入:
APP_ABI := x86
APP_CPPFLAGS := --std=c++11
NDK_TOOLCHAIN_VERSION := 4.8
1
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 isn't 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
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
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[1024] = {0};
sprintf (szTrace, "engine_handle_input num=[%d]", 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[nIdx] = pdwClr[s_nClr];
}
ANativeWindow_unlockAndPost(app->pendingWindow);
return 1;
}
return 0;
}
1
1、開啟 AndroidManifest.xml
2、開啟 Application 分頁
3、增加一個 Activity
如下所示:
2
1、選擇右邊的 Browse;
2、取消 "Display classes from sources of ..." 前面的核取方塊;
3、在搜尋欄輸入"na",選中列出來的 "NativeActivity"
4、點選OK
效果如下:
3
1、選中 android.app.nativeActivity
2、點選 add
3、選擇 Meta Data
4、點選Ok
4
輸入:
android:name="android.app.lib_name"
android:value="NativeTest"
5
1、選中 android.app.nativeActivity
2、點選 add
3、選擇 Intent Filter
4、點選Ok
6
1、選中 Intent Filter
2、點選 add
3、選擇 Action
4、點選Ok
5、設定 android:name="android.intent.action.MAIN"
7
1、選中 Intent Filter
2、點選 add
3、選擇 Category
4、點選Ok
5、設定 android:name="android.intent.category.LAUNCHER"
1
啟動模擬器,執行效果如下:
2
點選後效果:
3
這次整個程式完成1
整個程式實現:1、android 下面的純c/c++ 開發2、實現了多點觸控螢幕的功能3、實現了點選螢幕顏色的切換功能2
方便他人亦是方便自己,如果覺得還行就點下下邊的投票吧,這樣可以幫助其他人更快的找到解決問題的方法;有疑問的也可留言哦, 謝謝!
相關文章