DEV Community

Cover image for CAPI, Image Component Support Formats, Hybrid Development, Listening to Animation Status
kouwei qing
kouwei qing

Posted on

CAPI, Image Component Support Formats, Hybrid Development, Listening to Animation Status

【Daily HarmonyOS Next Knowledge】CAPI, Image Component Support Formats, Hybrid Development, Listening to Animation Status, and Context Acquisition Issues

1. Can HarmonyOS provide a demo for using CAPI to create custom components?

Demo sample code:

#include <functional>
#include "ArkUI/native_interface.h"
#include "ArkUI/native_node.h"
#include "ArkUI/native_type.h"
#include "drawing_canvas.h"
#include "drawing_color.h"
#include "drawing_path.h"
#include "drawing_pen.h"
#include "drawing_types.h"

struct UserCallback {
  std::function<void(ArkUI_NodeCustomEvent* event)> callback;
};

class ArkUINativeModule {
  public:
    ArkUINativeModule() {
      OH_ArkUI_QueryModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, nodeApi_);
      // Event handling through a unified entry function.
      nodeApi_->registerNodeCustomEventReceiver([](ArkUI_NodeCustomEvent* event) {
        auto* userData = reinterpret_cast<UserCallback*>(OH_ArkUI_NodeCustomEvent_GetUserData(event));
        userData->callback(event);
      });
    }

    ~ArkUINativeModule() {
      nodeApi_->unregisterNodeCustomEventReceiver();
    }

    static ArkUINativeModule* GetInstance() {
      static ArkUINativeModule nativeModule;
      return &nativeModule;
    }

    ArkUI_NativeNodeAPI_1* GetNodeAPI() {
      return nodeApi_;
    }

  private:
    ArkUI_NativeNodeAPI_1* nodeApi_ = nullptr;
};

class MyBadge {
  public:
    MyBadge() {
      nativeModule_ = ArkUINativeModule::GetInstance()->GetNodeAPI();
      handle_ = nativeModule_->createNode(ARKUI_NODE_CUSTOM);
      userCallback_ = new UserCallback();
      // Set the custom callback.
      userCallback_->callback = [this](ArkUI_NodeCustomEvent* event) {
        auto type = OH_ArkUI_NodeCustomEvent_GetEventType(event);
        switch (type) {
          case ARKUI_NODE_CUSTOM_EVENT_ON_MEASURE:
            OnMeasure(event);
            break;
          case ARKUI_NODE_CUSTOM_EVENT_ON_LAYOUT:
            OnLayout(event);
            break;
          case ARKUI_NODE_CUSTOM_EVENT_ON_FOREGROUND_DRAW:
            OnForegroundDraw(event);
            break;
          default:
            break;
        }
      };
    }
Enter fullscreen mode Exit fullscreen mode

2. Does the HarmonyOS Image component support HEIC format images?

Additionally, could you explain the internal implementation process of the Image component? Does it use hardware decoding or software decoding for image decoding?

  1. The Image component supports HEIC format.

    The internal implementation process of the component is as follows:

    • Default image loading: Data loading → Image decoding → Rendering and display
    • Network image loading: Download image → Data loading → Image decoding → Rendering and display
  2. The default decoding method is software decoding. If software decoding fails, hardware decoding will be used.

3. In HarmonyOS hybrid development, for custom ArkTS components, how does the CAPI side listen to event callbacks from custom components?

For event listening functions of custom components, refer to native_node: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/native__node_8h-V5

4. On HarmonyOS, does PAG support listening to changes in animation event status?

Reference: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-animator-V5

5. In HarmonyOS, is there a way to obtain the Context through getContext in the oncreate method?

In the oncreate method of uiability, calling getContext returns undefined, but directly accessing this.context works normally. Is there a way to obtain the Context through getContext in oncreate?

If the interface is called outside the current page, it may fail to track the Context of the instance, resulting in undefined.

Reference: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-getcontext-V5

Top comments (0)