NDK: 处理用户交互事件
2014-04-04 14:46
351 查看
在 android_main(struct android_app* state)函数里面设置输入事件处理函数:
state->onInputEvent = &handleInput;//设置输入事件的处理函数,如触摸响应
static int32_t handleInput(struct android_app* app, AInputEvent* event)
{
//获取消息的类型,消息有两种类型,一种是Motion类型一种是key类型。
int32_t lEventType = AInputEvent_getType(event);
switch(lEventType)
{
case AINPUT_EVENT_TYPE_MOTION:
//motion类型的消息的来源有两种,所以要获取消息的来源
switch(AInputEvent_getSource(event))
{
case AINPUT_SOURCE_TOUCHSCREEN:
//消息来源于触摸屏
break;
case AINPUT_SOURCE_TRACKBALL:
//消息来源于trackball
break;
}
break;
case AINPUT_EVENT_TYPE_KEY:
//消息来源于物理键盘或虚拟键盘,这个处理是一样的
break;
}
//知道来源与那里的时候,在判断具体是什么消息
int32_t id = AMotionEvent_getAction(event);
switch(id)
{
//触摸移动消息
case AMOTION_EVENT_ACTION_MOVE:
pNode->yaw(Ogre::Radian(Ogre::Degree(10.0f)));
break;
//触摸按下消息
case AMOTION_EVENT_ACTION_DOWN:
pNode->yaw(Ogre::Radian(Ogre::Degree(10.0f)));
break;
触摸弹起消息
case AMOTION_EVENT_ACTION_UP:
pNode->yaw(Ogre::Radian(Ogre::Degree(10.0f)));
break;
}
}
还有一些函数是获取坐标点的, 是绝对坐标:AMotionEvent_getX(),以屏幕左上角为原点。
AMotionEvent_getXOffset(event),返回0.0
AMotionEvent_getXPrecision(event)返回1.0
下面这两个函数用于多点触摸
AMotionEvent_getPointerCount(const
AInputEvent* motion_event);但是测试发现按下两个手指也返回1,不知道是不是硬件问题
AMotionEvent_getPointerId(const AInputEvent* motion_event, size_t pointer_index);不知道怎么用
AMotionEvent_getRawX(event,0);//这个返回的是相对于屏幕左上角的坐标,
AMotionEvent_getX(event,0);//和AMotionEvent_getRawX返回值一样
//这两个函数,如果第二个参数改成1之后,返回值总是为0.0
//说明这个pointer_index还是有用的。
终于弄明白这个point_index是什么意思了,这个值表示第几个手指,哈哈。
但是这个AMotionEvent_getPointerCount无论按下几个手指为什么老是返回1呢?
AMotionEvent_getOrientation(event,0);//总是返回0.0,按下两个手指有时有值。
输入设备有哪些:1 物理键盘,2 D-PAD,3 Trackball
输入类型:
////////////////////////////////////////////////////////////////////////////////////////////
enum {
/**
* Command from main thread: the AInputQueue has changed. Upon processing
* this command, android_app->inputQueue will be updated to the new queue
* (or NULL).
*/
APP_CMD_INPUT_CHANGED,
/**
* Command from main thread: a new ANativeWindow is ready for use. Upon
* receiving this command, android_app->window will contain the new window
* surface.
*/
APP_CMD_INIT_WINDOW,
/**
* Command from main thread: the existing ANativeWindow needs to be
* terminated. Upon receiving this command, android_app->window still
* contains the existing window; after calling android_app_exec_cmd
* it will be set to NULL.
*/
APP_CMD_TERM_WINDOW,
/**
* Command from main thread: the current ANativeWindow has been resized.
* Please redraw with its new size.
*/
APP_CMD_WINDOW_RESIZED,
/**
* Command from main thread: the system needs that the current ANativeWindow
* be redrawn. You should redraw the window before handing this to
* android_app_exec_cmd() in order to avoid transient drawing glitches.
*/
APP_CMD_WINDOW_REDRAW_NEEDED,
/**
* Command from main thread: the content area of the window has changed,
* such as from the soft input window being shown or hidden. You can
* find the new content rect in android_app::contentRect.
*/
APP_CMD_CONTENT_RECT_CHANGED,
/**
* Command from main thread: the app's activity window has gained
* input focus.
*/
APP_CMD_GAINED_FOCUS,
/**
* Command from main thread: the app's activity window has lost
* input focus.
*/
APP_CMD_LOST_FOCUS,
/**
* Command from main thread: the current device configuration has changed.
*/
APP_CMD_CONFIG_CHANGED,
/**
* Command from main thread: the system is running low on memory.
* Try to reduce your memory use.
*/
APP_CMD_LOW_MEMORY,
/**
* Command from main thread: the app's activity has been started.
*/
APP_CMD_START,
/**
* Command from main thread: the app's activity has been resumed.
*/
APP_CMD_RESUME,
/**
* Command from main thread: the app should generate a new saved state
* for itself, to restore from later if needed. If you have saved state,
* allocate it with malloc and place it in android_app.savedState with
* the size in android_app.savedStateSize. The will be freed for you
* later.
*/
APP_CMD_SAVE_STATE,
/**
* Command from main thread: the app's activity has been paused.
*/
APP_CMD_PAUSE,
/**
* Command from main thread: the app's activity has been stopped.
*/
APP_CMD_STOP,
/**
* Command from main thread: the app's activity is being destroyed,
* and waiting for the app thread to clean up and exit before proceeding.
*/
APP_CMD_DESTROY,
};
state->onInputEvent = &handleInput;//设置输入事件的处理函数,如触摸响应
static int32_t handleInput(struct android_app* app, AInputEvent* event)
{
//获取消息的类型,消息有两种类型,一种是Motion类型一种是key类型。
int32_t lEventType = AInputEvent_getType(event);
switch(lEventType)
{
case AINPUT_EVENT_TYPE_MOTION:
//motion类型的消息的来源有两种,所以要获取消息的来源
switch(AInputEvent_getSource(event))
{
case AINPUT_SOURCE_TOUCHSCREEN:
//消息来源于触摸屏
break;
case AINPUT_SOURCE_TRACKBALL:
//消息来源于trackball
break;
}
break;
case AINPUT_EVENT_TYPE_KEY:
//消息来源于物理键盘或虚拟键盘,这个处理是一样的
break;
}
//知道来源与那里的时候,在判断具体是什么消息
int32_t id = AMotionEvent_getAction(event);
switch(id)
{
//触摸移动消息
case AMOTION_EVENT_ACTION_MOVE:
pNode->yaw(Ogre::Radian(Ogre::Degree(10.0f)));
break;
//触摸按下消息
case AMOTION_EVENT_ACTION_DOWN:
pNode->yaw(Ogre::Radian(Ogre::Degree(10.0f)));
break;
触摸弹起消息
case AMOTION_EVENT_ACTION_UP:
pNode->yaw(Ogre::Radian(Ogre::Degree(10.0f)));
break;
}
}
还有一些函数是获取坐标点的, 是绝对坐标:AMotionEvent_getX(),以屏幕左上角为原点。
AMotionEvent_getXOffset(event),返回0.0
AMotionEvent_getXPrecision(event)返回1.0
下面这两个函数用于多点触摸
AMotionEvent_getPointerCount(const
AInputEvent* motion_event);但是测试发现按下两个手指也返回1,不知道是不是硬件问题
AMotionEvent_getPointerId(const AInputEvent* motion_event, size_t pointer_index);不知道怎么用
AMotionEvent_getRawX(event,0);//这个返回的是相对于屏幕左上角的坐标,
AMotionEvent_getX(event,0);//和AMotionEvent_getRawX返回值一样
//这两个函数,如果第二个参数改成1之后,返回值总是为0.0
//说明这个pointer_index还是有用的。
终于弄明白这个point_index是什么意思了,这个值表示第几个手指,哈哈。
但是这个AMotionEvent_getPointerCount无论按下几个手指为什么老是返回1呢?
AMotionEvent_getOrientation(event,0);//总是返回0.0,按下两个手指有时有值。
输入设备有哪些:1 物理键盘,2 D-PAD,3 Trackball
输入类型:
////////////////////////////////////////////////////////////////////////////////////////////
enum {
/**
* Command from main thread: the AInputQueue has changed. Upon processing
* this command, android_app->inputQueue will be updated to the new queue
* (or NULL).
*/
APP_CMD_INPUT_CHANGED,
/**
* Command from main thread: a new ANativeWindow is ready for use. Upon
* receiving this command, android_app->window will contain the new window
* surface.
*/
APP_CMD_INIT_WINDOW,
/**
* Command from main thread: the existing ANativeWindow needs to be
* terminated. Upon receiving this command, android_app->window still
* contains the existing window; after calling android_app_exec_cmd
* it will be set to NULL.
*/
APP_CMD_TERM_WINDOW,
/**
* Command from main thread: the current ANativeWindow has been resized.
* Please redraw with its new size.
*/
APP_CMD_WINDOW_RESIZED,
/**
* Command from main thread: the system needs that the current ANativeWindow
* be redrawn. You should redraw the window before handing this to
* android_app_exec_cmd() in order to avoid transient drawing glitches.
*/
APP_CMD_WINDOW_REDRAW_NEEDED,
/**
* Command from main thread: the content area of the window has changed,
* such as from the soft input window being shown or hidden. You can
* find the new content rect in android_app::contentRect.
*/
APP_CMD_CONTENT_RECT_CHANGED,
/**
* Command from main thread: the app's activity window has gained
* input focus.
*/
APP_CMD_GAINED_FOCUS,
/**
* Command from main thread: the app's activity window has lost
* input focus.
*/
APP_CMD_LOST_FOCUS,
/**
* Command from main thread: the current device configuration has changed.
*/
APP_CMD_CONFIG_CHANGED,
/**
* Command from main thread: the system is running low on memory.
* Try to reduce your memory use.
*/
APP_CMD_LOW_MEMORY,
/**
* Command from main thread: the app's activity has been started.
*/
APP_CMD_START,
/**
* Command from main thread: the app's activity has been resumed.
*/
APP_CMD_RESUME,
/**
* Command from main thread: the app should generate a new saved state
* for itself, to restore from later if needed. If you have saved state,
* allocate it with malloc and place it in android_app.savedState with
* the size in android_app.savedStateSize. The will be freed for you
* later.
*/
APP_CMD_SAVE_STATE,
/**
* Command from main thread: the app's activity has been paused.
*/
APP_CMD_PAUSE,
/**
* Command from main thread: the app's activity has been stopped.
*/
APP_CMD_STOP,
/**
* Command from main thread: the app's activity is being destroyed,
* and waiting for the app thread to clean up and exit before proceeding.
*/
APP_CMD_DESTROY,
};
相关文章推荐
- 37个简单实用的Ubuntu快捷键
- jQuery学习第一课
- cocos2dx三种定时器的使用以及停止schedule,scheduleUpdate,scheduleOnce
- maven2.0学习笔记
- jQuery学习第一课
- 精通JavaScript攻击框架:AttackAPI
- Google官方下拉刷新组件---SwipeRefreshLayout
- HDUOJ----2487Ugly Windows
- jQuery.noConflict();
- Java中byte与16进制字符串的互相转换
- mysql在大数据量下,如何优化SQL语句?
- ajax进行邮箱是否已经注册验证出的错
- C# 观察者模式(委托的使用)
- tcpdump
- 读《春秋》有感之十一:晋悼公休养生息
- array2json() - Convert PHP arrays to JSON
- 没有规则可以创建/framework.jar需要的目标framework-res.apk 停止
- NDK: NativeActivity原理
- 计算年龄
- 获取屏幕宽高和密度