harmony 鸿蒙Using Animations
Using Animations
Using Property Animations
The ArkUI framework primarily offers property animations through NDK APIs to implement component transition effects for appearance and disappearance. Additionally, frame animation capabilities from the ArkTS side can be bridged through the Node-API to achieve animation effects on the native side.
NOTE
Obtain this.getUIContext() from ArkTS and pass it to the native side.
On the native side, obtain the context using the OH_ArkUI_GetContextFromNapiValue API.
Animation property changes must be encapsulated within the callback of ArkUI_ContextCallback.
Ensure that the properties intended for animation have been set before the animation is executed.
A global animateTo explicit animation API is provided to specify transition effects for state changes caused by closure code. Like property animations, layout changes such as width and height adjustments are animated directly to their final states.
Obtain UIContext in the .ets file and pass this.getUIContext() as a parameter to the native API.
// createNativeNode is an API exposed on the native side. nativeNode.createNativeNode("xcomponentId", this.getUIContext());
Parse the UI context to convert the context object in C.
// Obtain the context passed from the ArkTS side. ArkUI_ContextHandle context = nullptr; // Determine whether the acquisition is successful based on code. auto code = OH_ArkUI_GetContextFromNapiValue(env, args[1], &context);
Obtain the ArkUI_NativeAnimateAPI_1 object.
// Obtain the ArkUI_NativeAnimateAPI. ArkUI_NativeAnimateAPI_1 *animateApi = nullptr; OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_ANIMATE, ArkUI_NativeAnimateAPI_1, animateApi);
Set the ArkUI_AnimateOption parameters using the provided C APIs.
ArkUI_AnimateOption *option = OH_ArkUI_AnimateOption_Create(); OH_ArkUI_AnimateOption_SetDuration(option, 2000); OH_ArkUI_AnimateOption_SetTempo(option, 1.1); OH_ArkUI_AnimateOption_SetCurve(option, ARKUI_CURVE_EASE); OH_ArkUI_AnimateOption_SetDelay(option, 20); OH_ArkUI_AnimateOption_SetIterations(option, 1); OH_ArkUI_AnimateOption_SetPlayMode(option, ARKUI_ANIMATION_PLAY_MODE_REVERSE); ArkUI_ExpectedFrameRateRange *range = new ArkUI_ExpectedFrameRateRange; range->min = 10; range->max = 120; range->expected = 60; OH_ArkUI_AnimateOption_SetExpectedFrameRateRange(option, range);
Set callback parameters.
// Define a user data struct. struct UserData{ int32_t data; }; UserData *onFinishUser = new UserData; onFinishUser->data= 101; // Create and set user data for the completion callback. ArkUI_AnimateCompleteCallback *completeCallback = new ArkUI_AnimateCompleteCallback; completeCallback->userData = onFinishUser; completeCallback->type = ARKUI_FINISH_CALLBACK_REMOVED; completeCallback->callback = [](void *userData) { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager", "CreateNativeNode onFinishCallback %{public}d", reinterpret_cast<AA *>(userData)->a); }; // User data UserData *eventUser = new UserData ; eventUser->data= 201; static bool isback = true; ArkUI_ContextCallback *update = new ArkUI_ContextCallback; update->userData = eventUser; update->callback = [](void *user) { OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager", "CreateNativeNode animateTo %{public}d", reinterpret_cast<UserData*>(user)->data); // Example of changing width and height properties if (isback) { ArkUI_NumberValue custom_widthValue[] = {200}; ArkUI_AttributeItem custom_widthItem = {custom_widthValue, 1}; ArkUI_NumberValue custom_heightValue1[] = {80}; ArkUI_AttributeItem custom_heightItem1 = {custom_heightValue1, 1}; nodeAPI->setAttribute(textInput, NODE_WIDTH, &custom_widthItem); nodeAPI->setAttribute(textInput, NODE_HEIGHT, &custom_heightItem1); } else { ArkUI_NumberValue custom_widthValue[] = {100}; ArkUI_AttributeItem custom_widthItem = {custom_widthValue, 1}; ArkUI_NumberValue custom_heightValue1[] = {40}; ArkUI_AttributeItem custom_heightItem1 = {custom_heightValue1, 1}; nodeAPI->setAttribute(textInput, NODE_WIDTH, &custom_widthItem); nodeAPI->setAttribute(textInput, NODE_HEIGHT, &custom_heightItem1); } }; // Execute the animation with the set options and callbacks. animateApi->animateTo(context, option, update, completeCallback);
Using Component Appearance/Disappearance Transitions
Use NODE_XX_TRANSITION properties (where XX can be OPACITY, TRANSLATE, SCALE, ROTATE, or MOVE) to configure transition effects for components, enhancing the user experience when components are added to or removed from containers. The NODE_TRANSFORM_CENTER property sets the center point for animations including NODE_SCALE_TRANSITION and NODE_ROTATE_ROTATE.
- Design an interactive UI with a button to manage the addition and removal of transition nodes. For details about how to obtain and use the ArkUI_NodeContentHandle node, see Integrating with ArkTS Pages. “` constexpr int32_t BUTTON_CLICK_ID = 1; bool flag = false; ArkUI_NodeHandle parrentNode; ArkUI_NodeHandle childNode; ArkUI_NodeHandle buttonNode;
void mainViewMethod(ArkUI_NodeContentHandle handle)
{
ArkUI_NativeNodeAPI_1 *nodeAPI = reinterpret_cast
2. Create a node with **Transition** properties that play a transition animation when the target node is mounted or unmounted.
ArkUI_NodeHandle CreateChildNode() {
ArkUI_NativeNodeAPI_1 *nodeAPI = reinterpret_cast
3. Add logic for mounting and unmounting the transition node within the **Button** component event callback to control the appearance and disappearance of the transition node.
void OnButtonShowClicked(ArkUI_NodeEvent* event)
{
if (!event) {
return;
}
if (!childNode) {
childNode = CreateChildNode();
}
ArkUI_NativeNodeAPI_1 *nodeAPI = reinterpret_cast
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Atomic Service Full Screen Launch Component (FullScreenLaunchComponent)
harmony 鸿蒙Arc Button (ArcButton)
harmony 鸿蒙Frame Animation (ohos.animator)
harmony 鸿蒙Implementing Property Animation
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦