1# Native XComponent 2 3### 介绍 4 5本示例中主要介绍开发者如何使用Native XComponent接口来获取NativeWindow实例、获取布局/事件信息、注册事件回调并通过OpenGL/EGL实现在页面上绘制形状。功能主要包括点击按钮绘制一个五角星,并可以通过点击XComponent区域改变五角星的颜色。 6 7### 效果预览 8 9| 主页 | 绘制五角星 | 改变颜色 | 10| ------------------------------------ | --------------------------------------------- | --------------------------------------------------- | 11|  |  |  | 12 13使用说明 14 151. 安装编译生成的hap包,并打开应用。 16 172. 点击页面底部“Draw Star”按钮,页面将绘制一个五角星。 18 193. 点击XComponent组件区域(页面中灰色区域)改变五角星颜色。 20 21 22### 工程目录 23 24``` 25├──entry/src/main 26│ ├──cpp // C++代码区 27│ │ ├──CMakeLists.txt // CMake配置文件 28│ │ ├──napi_init.cpp // Napi模块注册 29│ │ ├──common 30│ │ │ └──common.h // 常量定义文件 31│ │ ├──manager // 生命周期管理模块 32│ │ │ ├──plugin_manager.cpp 33│ │ │ └──plugin_manager.h 34│ │ ├──render // 渲染模块 35│ │ │ ├──egl_core.cpp 36│ │ │ ├──egl_core.h 37│ │ │ ├──plugin_render.cpp 38│ │ │ └──plugin_render.h 39│ ├──ets // ets代码区 40│ │ ├──entryability 41│ │ │ └──EntryAbility.ts // 程序入口类 42│ │ └──pages // 页面文件 43│ │ └──Index.ets // 主界面 44| ├──resources // 资源文件目录 45``` 46 47### 具体实现 48 49通过在IDE中创建Native c++ 工程,在c++代码中定义对外接口为drawPattern,在js侧调用该接口可在页面上绘制出一个三角形。 50 51在XComponent的OnSurfaceCreated回调中获取NativeWindow实例并初始化EGL环境。调用OH_NativeXComponent_GetXComponentSize接口获取XComponent的宽高,并以此为输入调用EGL相关的绘制接口在NativeWindow上绘制出一个五角星。在DispatchTouchEvent回调中再次调用EGL相关的绘制接口在NativeWindow上绘制出一个大小相同、颜色不同的五角星,以达到点击后改变颜色的目的。 52 53源码参考:[render目录](entry/src/main/cpp/render)下的文件。 54 55涉及到的相关接口: 56 57| 接口名 | 描述 | 58| ------------------------------------------------------------ | ------------------------------------------------------- | 59| OH_NativeXComponent_GetXComponentId(OH_NativeXComponent* component, char* id, uint64_t* size) | 获取XComponent的id。 | 60| OH_NativeXComponent_GetXComponentSize(OH_NativeXComponent* component, const void* window, uint64_t* width, uint64_t* height) | 获取XComponent持有的surface的大小。 | 61| OH_NativeXComponent_GetXComponentOffset(OH_NativeXComponent* component, const void* window, double* x, double* y) | 获取XComponent持有的surface相对窗口左上角的偏移量。 | 62| OH_NativeXComponent_GetTouchEvent(OH_NativeXComponent* component, const void* window, OH_NativeXComponent_TouchEvent* touchEvent) | 获取由XComponent触发的触摸事件。 | 63| OH_NativeXComponent_GetTouchPointToolType(OH_NativeXComponent* component, uint32_t pointIndex, OH_NativeXComponent_TouchPointToolType* toolType) | 获取XComponent触摸点的工具类型。 | 64| OH_NativeXComponent_GetTouchPointTiltX(OH_NativeXComponent* component, uint32_t pointIndex, float* tiltX) | 获取XComponent触摸点处相对X轴的倾斜角度。 | 65| OH_NativeXComponent_GetTouchPointTiltY(OH_NativeXComponent* component, uint32_t pointIndex, float* tiltY) | 获取XComponent触摸点处相对Y轴的倾斜角度。 | 66| OH_NativeXComponent_GetMouseEvent(OH_NativeXComponent* component, const void* window, OH_NativeXComponent_MouseEvent* mouseEvent) | 获取由XComponent触发的鼠标事件。 | 67| OH_NativeXComponent_RegisterCallback(OH_NativeXComponent* component, OH_NativeXComponent_Callback* callback) | 为此OH_NativeXComponent实例注册生命周期和触摸事件回调。 | 68| OH_NativeXComponent_RegisterMouseEventCallback(OH_NativeXComponent* component, OH_NativeXComponent_MouseEvent_Callback* callback) | 为此OH_NativeXComponent实例注册鼠标事件回调。 | 69| OH_NativeXComponent_RegisterFocusEventCallback(OH_NativeXComponent* component, void (*callback)(OH_NativeXComponent* component, void* window)) | 为此OH_NativeXComponent实例注册获得焦点事件回调。 | 70| OH_NativeXComponent_RegisterKeyEventCallback(OH_NativeXComponent* component, void (*callback)(OH_NativeXComponent* component, void* window)) | 为此OH_NativeXComponent实例注册按键事件回调。 | 71| OH_NativeXComponent_RegisterBlurEventCallback(OH_NativeXComponent* component, void (*callback)(OH_NativeXComponent* component, void* window)) | 为此OH_NativeXComponent实例注册失去焦点事件回调。 | 72| OH_NativeXComponent_GetKeyEvent(OH_NativeXComponent* component, OH_NativeXComponent_KeyEvent** keyEvent) | 获取由XComponent触发的按键事件。 | 73| OH_NativeXComponent_GetKeyEventAction(OH_NativeXComponent_KeyEvent* keyEvent, OH_NativeXComponent_KeyAction* action) | 获取按键事件的动作。 | 74| OH_NativeXComponent_GetKeyEventCode(OH_NativeXComponent_KeyEvent* keyEvent, OH_NativeXComponent_KeyCode* code) | 获取按键事件的键码值。 | 75| OH_NativeXComponent_GetKeyEventSourceType(OH_NativeXComponent_KeyEvent* keyEvent, OH_NativeXComponent_EventSourceType* sourceType) | 获取按键事件的输入源类型。 | 76| OH_NativeXComponent_GetKeyEventDeviceId(OH_NativeXComponent_KeyEvent* keyEvent, int64_t* deviceId) | 获取按键事件的设备ID。 | 77| OH_NativeXComponent_GetKeyEventTimestamp(OH_NativeXComponent_KeyEvent* keyEvent, int64_t* timestamp) | 获取按键事件的时间戳。 | 78 79### 相关权限 80 81不涉及。 82 83### 依赖 84 85不涉及。 86 87### 约束与限制 88 891. 本示例仅支持标准系统上运行,支持设备:RK3568 90 912. 本示例为Stage模型,支持API10版本SDK,SDK版本号(API Version 10 Release),镜像版本号(4.0 Release) 92 933. 本示例需要使用DevEco Studio 版本号(4.0 Release)及以上版本才可编译运行 94 95### 下载 96 97如需单独下载本工程,执行如下命令: 98 99``` 100git init 101git config core.sparsecheckout true 102echo code/BasicFeature/Native/NdkXComponent/ > .git/info/sparse-checkout 103git remote add origin https://gitee.com/openharmony/applications_app_samples.git 104git pull origin master 105``` 106