在Android NDK系列(一)手动搭建Native Project 创建的Native工程中,是可以接收输入事件的,只需在android_main中注册输入事件的处理函数,当触摸屏幕后,handleInputEvent函数便会调用,代码如下。
static int32_t handleInputEvent(struct android_app* app, AInputEvent* event) { LOGI("android_main handleInputEvent"); if(AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION && AInputEvent_getSource(event) == AINPUT_SOURCE_TOUCHSCREEN){ int32_t action = AMotionEvent_getAction(event); float x = AMotionEvent_getX(event,0); float y = AMotionEvent_getY(event,0); switch(action){ case AMOTION_EVENT_ACTION_DOWN: LOGI("android_main AMOTION_EVENT_ACTION_DOWN (x %f, y %f)", x,y); break; case AMOTION_EVENT_ACTION_MOVE: LOGI("android_main AMOTION_EVENT_ACTION_MOVE"); break; case AMOTION_EVENT_ACTION_UP: LOGI("android_main AMOTION_EVENT_ACTION_UP"); break; default: break; } } return 0; } void android_main(struct android_app* app) { app->onInputEvent = handleInputEvent; }
本文主要介绍触摸事件是如何传递过来的,主要分为以下两个部分。
一、创建输入队列
要收到输入事件,首先要创建输入队列,ViewRootImpl在处理setView请求时开始创建输入队列,代码如下。
public final class ViewRootImpl implements ViewParent, View.AttachInfo.Callbacks, ThreadedRenderer.DrawCallbacks, AttachedSurfaceControl { public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView, int userId) { if (mInputQueueCallback != null) { mInputQueue = new InputQueue(); mInputQueueCallback.onInputQueueCreated(mInputQueue); } } } public final class InputQueue { public InputQueue() { mPtr =
还没有评论,来说两句吧...