在 HarmonyOS Java 端实现气泡聊天框,与 Android 上的9图(Nine-Patch)有相似的实现方式。在 HarmonyOS 中,可以使用 ShapeElement 和 ElementContainer 来创建和管理可伸缩的气泡背景。


下面,提供一个简单的示例代码,演示如何在 HarmonyOS 中实现气泡聊天框。


示例代码

Step 1: 创建一个 Nine-Patch 资源文件

首先,创建一个 Nine-Patch 图片资源。在 HarmonyOS 中,可以直接使用 Android Studio 工具生成 Nine-Patch 图像,并将其复制到 HarmonyOS 项目的 resources/base/media 文件夹中。


Step 2: 在 Java 代码中使用 ShapeElement

使用 ShapeElement 或者 PixelMapElement 来加载 Nine-Patch 图片资源,并设置给组件的背景。


import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.agp.components.*;
import ohos.agp.components.element.PixelMapElement;
import ohos.app.Context;
import ohos.media.image.PixelMap;

public class ChatBubble extends DirectionalLayout {
    private Context context;

    public ChatBubble(Context context) {
        super(context);
        this.context = context;
        init();
    }

    private void init() {
        // 设置布局方向
        setOrientation(VERTICAL);

        // 加载 Nine-Patch 图片资源
        PixelMapElement ninePatchElement = new PixelMapElement(getPixelMap("resources/base/media/bubble.9.png"));
        
        // 创建 Text 组件,用于显示聊天文本
        Text chatText = new Text(context);
        chatText.setText("Hello, this is a sample chat bubble!");
        chatText.setTextSize(50);
        chatText.setPadding(50, 20, 50, 20);
        
        // 将 Nine-Patch 背景设置给 Text 组件
        chatText.setBackground(ninePatchElement);

        // 将 Text 组件添加到当前布局
        addComponent(chatText);
    }

    private PixelMap getPixelMap(String path) {
        try {
            ResourceManager resourceManager = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext().getResourceManager();
            if (resourceManager != null) {
                Resource resource = resourceManager.getRawFileEntry(path).openRawFile();
                return PixelMap.decodeStream(resource, null);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.

Step 3: 在布局文件或程序中使用 ChatBubble

在主界面或者其他地方实例化并使用 ChatBubble:


import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentContainer;
import ohos.agp.window.service.Window;

public class MainAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
        
        // 获取窗口并设置内容视图
        Window window = getWindow();
        ComponentContainer rootLayout = (ComponentContainer) LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_ability_main, null, false);

        // 实例化单个聊天气泡
        ChatBubble chatBubble = new ChatBubble(this);
        
        // 添加聊天气泡到根布局
        rootLayout.addComponent(chatBubble);

        // 设置窗口显示内容
        window.setUIContent(rootLayout);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

关键点解释

Nine-Patch 图片资源:

请确保你的项目中包含了正确格式的 Nine-Patch 图片,例如 bubble.9.png。可以通过 Android Studio 的 Draw 9-Patch 工具生成。


PixelMapElement:

使用 PixelMapElement 加载和展示 Nine-Patch 图片资源。


Text 组件背景:

将 PixelMapElement 设置为 Text 组件的背景,实现气泡效果。


总结

通过上述方法,可以在 HarmonyOS 中实现类似于 Android 的 Nine-Patch 气泡聊天框效果。