鸿蒙(HarmonyOS)应用开发实战——图片缩放效果实现

往期知识点整理

介绍

图片预览在应用开发中是一种常见场景,在诸如QQ、微信、微博等应用中均被广泛使用。本模块基于Image组件实现了简单的图片预览功能。

使用说明:

  1. 双指捏合对图片进行缩放
  2. 双击图片进行图片的大小切换,在放大状态下,双击可恢复默认状态
  3. 图片在放大模式下,滑动图片查看图片的对应位置

效果图预览

实现思路

  1. 使用matrix实现图片的缩放。
    @State matrix: matrix4.Matrix4Transit = matrix4.identity().copy();
    Image(this.imagePixelMap)
        .transform(this.matrix)
  1. 使用offset属性对图片进行偏移。
    @State imageOffsetInfo: OffsetModel = new OffsetModel(0, 0);
    Image(this.imagePixelMap)
        .offset({
            x: this.imageOffsetInfo.currentX,
            y: this.imageOffsetInfo.currentY
        })
  1. Image的objectFit属性设置为Cover,锁定图片宽高比,并使其能够超出父组件边界显示。
   Image(this.imagePixelMap)
       .objectFit(ImageFit.Cover)
  1. 提前计算图片信息,并通过Image的宽或高配合aspectRatio设置默认尺寸。
   initCurrentImageInfo(): void {
       this.matrix = matrix4.identity().copy();
       const imageSource: image.ImageSource = image.createImageSource(this.imageUri);
       imageSource.getImageInfo(0).then((data: image.ImageInfo) => {
           this.imageWHRatio = data.size.width / data.size.height;
           this.imageDefaultSize = this.calcImageDefaultSize(this.imageWHRatio, windowSizeManager.get());
           if (this.imageDefaultSize.width === windowSizeManager.get().width) {
               this.fitWH = "width";
           } else {
               this.fitWH = "height";
           }
           this.imageScaleInfo.maxScaleValue += this.fitWH === "width" ? 
               (windowSizeManager.get().height / this.imageDefaultSize.height) : 
               (windowSizeManager.get().width / this.imageDefaultSize.width);
       }).catch((err: BusinessError) => {
           console.error(`[error][getImageInfo]${err.message}`);
       });
       imageSource.createPixelMap().then((data: image.PixelMap) => {
           this.imagePixelMap = data;
       }).catch((err: BusinessError) => {
           console.error(`[error][createPixelMap]${err.message}`);
       });
   }
   
   Image(this.imagePixelMap)
      .width(this.fitWH === "width" ? $r("app.string.image_default_width") : undefined)
      .height(this.fitWH === "height" ? $r("app.string.image_default_height") : undefined)
      .aspectRatio(this.imageWHRatio)

工程结构&模块类型

imageviewer                                     // har类型
|---constants                                   // 常量
|---model                                       // 模型层-自定义数据模型
|---utils                                       // 工具类
|---view                                        // 视图层-图片预览方案涉及的主要组件
|---|---ImageViewerView.ets                     // 视图层-入口
|---|---ImageItemView.ets                       // 视图层-单张图片的显示组件

最后

总是有很多小伙伴反馈说:鸿蒙开发不知道学习哪些技术?不知道需要重点掌握哪些鸿蒙开发知识点? 为了解决大家这些学习烦恼。在这准备了一份很实用的鸿蒙全栈开发学习路线与学习文档给大家用来跟着学习。

针对一些列因素,整理了一套纯血版鸿蒙(HarmonyOS Next)全栈开发技术的学习路线,包含了鸿蒙开发必掌握的核心知识要点,内容有(OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、OpenHarmony驱动开发、系统定制移植……等)技术知识点。

《鸿蒙 (Harmony OS)开发学习手册》(共计892页):https://gitcode.com/HarmonyOS_MN/733GH/overview

如何快速入门?

1.基本概念
2.构建第一个ArkTS应用
3.……

在这里插入图片描述

鸿蒙开发面试真题(含参考答案):

在这里插入图片描述

OpenHarmony 设备开发学习手册:https://gitcode.com/HarmonyOS_MN/733GH/overview

图片

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值