代码拉取完成,页面将自动刷新
XComponent 提供了应用在 native 侧调用 OpenGLES 图形接口的能力,本文主要介绍如何配合 Vsync 事件,完成自定义动画。在这种实现方式下,自定义动画的绘制不在 UI 主线程中完成,即使主线程卡顿,动画效果也不会受影响。
find_library( # Sets the name of the path variable.
libvsync-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
native_vsync )
#include <native_vsync/native_vsync.h>
SyncParam *param = new SyncParam();
param->eglCore = this;
param->window = window;
mVsync = OH_NativeVSync_Create(GAME_SYNC_NAME, 3);
if (!mVsync) {
LOGE("EGLCore:: Create mVsync failed");
return;
}
OH_NativeVSync_RequestFrame(
mVsync,
[](long long timestamp, void *data) {
LOGI("EGLCore::OH_NativeVSync_RequestFrame timestamp = %{public}lld", timestamp);
SyncParam *syncParam = (SyncParam *)data;
if (syncParam == nullptr) {
return;
}
EGLCore *eglCore = syncParam->eglCore;
void *window = syncParam->window;
if (eglCore == nullptr || window == nullptr) {
return;
}
eglCore->mEglWindow = static_cast<EGLNativeWindowType>(window);
...
eglCore->DrawSquare();
eglCore->switchSpecular();
eglCore->switchAmbient();
},
param
);
此处需要注意,frame callback 回调的执行线程不是当前申请 OH_NativeVSync_RequestFrame 的线程,而是 vsync 实例中申请的 eventlooper 线程。所以,egl Window 的初始化也需要放到 callback 中创建。
XcomponentVsync // har类型
|---cpp // native xcomponent配合vsync实现自定义动画
|---pages
| |---XcomponentVsync.ets // Xcomponent 组件加载自定义动画
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。