美文网首页@IT·互联网鸿蒙(HarmonyOS)开发知识
基于 OpenHarmony Matisse 三方件开发指南

基于 OpenHarmony Matisse 三方件开发指南

作者: 迪士尼在逃程序员 | 来源:发表于2024-08-05 17:56 被阅读0次

    1. Matisse 组件功能介绍

    1.1. 功能介绍:

    Matisse 组件是一个图片选择框架,实现图片的选择及使用相机进行拍摄,最后将选择
    或拍摄的照片显示在 ability 中(目前初版还未实现所有功能)。

    1.2. 模拟器上运行效果:

    2. Matisse 使用方法

    2.1. 新建工程,增加组件 Har 包依赖

    在应用模块中添加 HAR,只需要将 Matisse.har 复制到 entry\libs 目录下即可(由于 build.gradle
    中已经依赖的 libs 目录下的*.har,因此不需要在做修改)。

    2.2. 修改主页面代码成为请求方

    在 MainAbilitySlice 中 构 造 Intent 以 及 包 含 Action 的 Operation 对 象 , 并 调 用
    startAbilityForResult()方法发起请求。然后重写 onAbilityResult()回调方法,对请求结果进行处
    理。

    private void pickPicture() {
        Intent intent = new Intent();
          Operation operation = new Intent.OperationBuilder()
            .withAction("action.pick_pic")
            .withBundleName("com.example.matisse")
            .withAbilityName("com.example.matisse.MatisseAbility")
            .build();
        intent.setOperation(operation);
        startAbilityForResult(intent, REQ_CODE_PICK_PIC);
    }
    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData)
    {
        switch (requestCode) {
        case REQ_CODE_PICK_PIC:
        // Do something with result.
        ...
        return;
        default:
        ...
      }
    }
    

    3. Matisse 开发实现

    3.1. 新建一个 Module

    新建一个 Module,类型选择 HarmonyOS Library,模块名为 Matisse,如图

    3.2. 获得媒体库所有图片

    新建一个 MatisseAbility 在其 slice 的 onStart 方法中申请权限并配制 config.json:

    @Override
    public void onStart(Intent intent) {
    super.onStart(intent);
    super.setUIContent(ResourceTable.Layout_ability_matisse);
    String[] per={"ohos.permission.READ_USER_STORAGE"};
    requestPermissionsFromUser(per,0);
    }
    

    为了获得媒体库所有图片(的 URI),需要定义一个 DataAbilityHelper,并设置 resultSet,代码如下:

    DataAbilityHelper helper = DataAbilityHelper.creator(this);
    try {
    ResultSet resultSet =
    helper.query(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI, null, null);
    while (resultSet != null && resultSet.goToNextRow()) {
    int mid =
    resultSet.getInt(resultSet.getColumnIndexForName(AVStorage.Images.Media.ID))
    ;
    Uri uri =
    Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI,
    "" + mid);
    FileDescriptor fileDescriptor = null;
    fileDescriptor = helper.openFile(uri, "r");
    ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
    decodingOptions.desiredSize = new Size(180, 320);
    ImageSource imageSource = ImageSource.create(fileDescriptor, null);
    PixelMap pixelMap =
    imageSource.createThumbnailPixelmap(decodingOptions, true);
    uris.add(new UriModel(uri, pixelMap));
    }
    } catch (DataAbilityRemoteException | FileNotFoundException e) {
    e.printStackTrace();
    }
    

    由于 Listcontainer 目前不支持 grid 布局,所以取得所有图片的 uri 后对结果进行分组:

    if(uris.size()>3) {
    List<List<UriModel>> newList = arraySplitUtil.splistList(uris, 3);
    for (List<UriModel> uriModel : newList) {
    nestedLists.add(new NestedList(uriModel));
    }
    }else {
    nestedLists.add(new NestedList(uris));
    }
    

    3.3. 新建 Listconainer 并使用嵌套方式实现相册视图:

    listContainer = (ListContainer)
    findComponentById(ResourceTable.Id_galley);
    UltimateProvider ultimateProvider=new
    UltimateProvider(nestedLists,getContext());
    listContainer.setItemProvider(ultimateProvider);
    

    这里使用了三方件 UltimateProvider 减少代码量编写。

    3.4. 点击事件和回调处理

    使用 DependentLayout 在图片布局右上方加上 checkbox, 代码中 id_icon 为图片:

    checkbox=(Checkbox)findComponentById(ResourceTable.Id_checked);
    checkbox.setChecked(false);
    icon.setClickedListener(new Component.ClickedListener() {
    @Override
    public void onClick(Component component) {
    checkbox.toggle();
    // getModel().getUri()
    }
    });
    checkbox.setCheckedStateChangedListener((view, state) -> {
    if (state) {
    MatisseAbilitySlice.selectedSet.add(getModel());
    }else {
    MatisseAbilitySlice.selectedSet.remove(getModel());
    }
    });
    

    回调:在 config.json 中增加"action.pick_pic"并在 MatisseAbility 中添加如下路由:

    addActionRoute("action.pick_pic", MatisseAbilitySlice.class.getName());
    

    新建使用按钮,并增加如下代码:

    use.setClickedListener(new Component.ClickedListener() {
    @Override
    public void onClick(Component component) {
    if (selectedSet.size()==0){
    return;
    }
    else {
    Intent resultIntent = new Intent();
    resultIntent.setParam("model", (Serializable) selectedSet);
    // Intent resultIntent = new Intent();
    setResult(resultIntent);
    }
    }
    });
    

    3.5. 编译 HAR 包

    利用 Gradle 可以将 HarmonyOS Library 库模块构建为 HAR 包,构建 HAR 包的方法如下:

    在 Gradle 构建任务中,双击 PackageDebugHar 或PackageReleaseHar 任务,构建 Debug 类型 或 Release 类型的 HAR。

    待构建任务完成后,可以在工程目录中的 Matisse> bulid > outputs > har 目录中,获取生成的HAR 包。

    写在最后

    • 如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:
    • 点赞,转发,有你们的 『点赞和评论』,才是我创造的动力。
    • 关注小编,同时可以期待后续文章ing🚀,不定期分享原创知识。
    • 想要获取更多完整鸿蒙最新学习知识点,请移步前往小编:https://gitee.com/MNxiaona/733GH/blob/master/jianshu

    相关文章

      网友评论

        本文标题:基于 OpenHarmony Matisse 三方件开发指南

        本文链接:https://www.haomeiwen.com/subject/imoatjtx.html