文档开发者中心
我的
|

ArkUI (方舟UI框架)

UI

HarmonyOS 5

HarmonyOS 图片可以通过手势进行放大缩小,并且可以通过手势进行滑动 (API12+)

HarmonyOS 图片可以通过手势进行放大缩小,并且可以通过手势进行滑动 (API12+)

点赞
1
收藏
1
回复
1
分享
举报
浏览123 发布于2026-04-02 11:25未知归属地
全部评论
最多点赞
最新发布
最早发布

可以参考以下链接:结合拖拽手势实现简易电子相册。

简单实现可以参考:

1、创建图片预览页面,实现在点击图片时,进入图片预览页面,进行大图展示及移动缩放操作。

2、通过在图片预览页面设置组合手势GestureMode控制Image组件的scale属性从而控制图片缩放,以及控制offset/position属性从而控制Image组件的位置。

代码实现方式,参考如下:

1、创建评论浏览主页面。

收起
自动换行
深色代码主题
复制
import { ImagePreviewBuilder, ImagePreviewParam } from './ImagePreview';

@Entry
@Component
struct ImagePage {
  @State imageList: (string | Resource)[] = [
    $r('app.media.startIcon'),
    $r('app.media.xxx'),
    $r('app.media.startIcon')
  ]
  @Provide pathStack: NavPathStack = new NavPathStack()

  @Builder
  PageMap(name: string) {
    if (name === 'ImagePreview') {
      ImagePreviewBuilder()
    }
  }

  build() {
    Navigation(this.pathStack) {
      Column() {
        List() {
          ForEach(this.imageList, (item: string | Resource, index: number) => {
            ListItem() {
              Image(item)
                .width('100%')
                .aspectRatio(1)
                .onClick(() => {
                  this.pathStack.pushPath({
                    name: 'ImagePreview',
                    param: { imageList: this.imageList, active: index } as ImagePreviewParam
                  })
                })
            }
            .width('50%')
            .aspectRatio(1)
          })
        }
        .layoutWeight(1)
        .width('100%')
      }
      .padding({
        top: AppStorage.get<number>('topRectHeight') || 0
      })
    }
    .navDestination(this.PageMap)
    .height('100%')
    .width('100%')
    .hideTitleBar(true)
  }
}

2、创建图片预览页面。

内部用到的接口:

收起
自动换行
深色代码主题
复制
export interface ImagePreviewParam {
  imageList: (string | Resource)[]
  active: number
}

export interface imageSize {
  width: number
  height: number
}

export interface ImageSizeType {
  width: number
  height: number
  scale: number
  offsetX: number
  offsetY: number
  offsetStartX: number
  offsetStartY: number
  dragOffsetX: number
  dragOffsetY: number
}

核心逻辑实现代码:

收起
自动换行
深色代码主题
复制
@Component
struct ImagePreview {
  private imageListSize: imageSize[] = []
  private defaultScale: number = 1
  @Consume pathStack: NavPathStack
  @State imageList: (string | Resource)[] = []
  @State active: number = 0
  @State containerHeight: number = 0
  @State containerWidth: number = 0
  @State disabledSwipe: boolean = false
  @State activeImage: ImageSizeType = {
    width: 0,
    height: 0,
    scale: 1,
    offsetX: 0,
    offsetY: 0,
    offsetStartX: 0,
    offsetStartY: 0,
    dragOffsetX: 0,
    dragOffsetY: 0,
  }

  build() {
    NavDestination() {
      Swiper() {
        ForEach(this.imageList, (item: string, i: number) => {
          Image(item)
            .width(`calc(100% * ${this.activeImage.scale})`)
            .height(`calc(100% * ${this.activeImage.scale})`)
            .objectFit(ImageFit.Contain)
            .draggable(false)
            .scale(this.active === i ? { x: this.activeImage.scale, y: this.activeImage.scale } : null)
            .offset(this.active === i ? { x: this.activeImage.offsetX, y: this.activeImage.offsetY } : null)
            .onComplete(e => {
              if (e?.loadingStatus) {
                this.imageListSize[i] = {
                  width: px2vp(Number(e.contentWidth)),
                  height: px2vp(Number(e.contentHeight))
                }
                if (this.active === i) {
                  this.activeImage.width = this.imageListSize[i].width
                  this.activeImage.height = this.imageListSize[i].height
                }
              }
            })
        }, (item: string, i) => i.toString())
      }
      .width('100%')
      .height('100%')
      .index(this.active)
      .backgroundColor('#000')
      .parallelGesture(
        GestureGroup(GestureMode.Exclusive,
          PinchGesture({ fingers: 2 })
            .onActionStart((event) => {
              this.defaultScale = this.activeImage.scale
            })
            .onActionUpdate((event) => {
              let scale = event.scale * this.defaultScale
              if (scale <= 4 && scale >= 1) {
                this.activeImage.offsetX = this.activeImage.offsetX / (this.activeImage.scale - 1) * (scale - 1) || 0
                this.activeImage.offsetY = this.activeImage.offsetY / (this.activeImage.scale - 1) * (scale - 1) || 0
                this.activeImage.scale = scale
              }
              this.disabledSwipe = this.activeImage.scale > 1
            })
            .onActionEnd((event) => {
              this.disabledSwipe = this.activeImage.scale > 1
            })
            .onActionCancel(() => {
              this.disabledSwipe = this.activeImage.scale > 1
            }),
          PanGesture()
            .onActionStart(event => {
              this.activeImage.dragOffsetX = event.fingerList[0].globalX
              this.activeImage.dragOffsetY = event.fingerList[0].globalY
            })
            .onActionUpdate((event) => {
              if (this.activeImage.scale === 1) {
                return
              }
              let offsetX = event.fingerList[0].globalX - this.activeImage.dragOffsetX +
              this.activeImage.offsetStartX
              let offsetY = event.fingerList[0].globalY - this.activeImage.dragOffsetY +
              this.activeImage.offsetStartY
              if (this.activeImage.width * this.activeImage.scale > this.containerWidth &&
                (this.activeImage.width * this.activeImage.scale - this.containerWidth) / 2 >=
                Math.abs(offsetX)) {
                this.activeImage.offsetX = offsetX
              }
              if (this.activeImage.height * this.activeImage.scale >
              this.containerHeight &&
                (this.activeImage.height * this.activeImage.scale - this.containerHeight) / 2 >=
                Math.abs(offsetY)) {
                this.activeImage.offsetY = offsetY
              }
              if ((this.activeImage.width * this.activeImage.scale - this.containerWidth) / 2 < Math.abs(offsetX)) {
                this.disabledSwipe = false
              }
            })
            .onActionEnd((event) => {
              this.activeImage.offsetStartX = this.activeImage.offsetX
              this.activeImage.offsetStartY = this.activeImage.offsetY
            })
            .onActionCancel(() => {
              this.activeImage.offsetStartX = this.activeImage.offsetX
              this.activeImage.offsetStartY = this.activeImage.offsetY
            }),
          TapGesture({ count: 2 })// 双击手势
            .onAction(() => {
              if (this.activeImage.scale > 1) {
                this.activeImage.scale = 1
                this.activeImage.offsetX = 0
                this.activeImage.offsetY = 0
                this.activeImage.offsetStartX = 0
                this.activeImage.offsetStartY = 0
                this.disabledSwipe = false
              } else {
                this.activeImage.scale = 2
                this.disabledSwipe = true
              }
            }),
          TapGesture({ count: 1 })// 单击手势
            .onAction(() => {
              this.pathStack?.pop()
            }),
        )
      )
      .disableSwipe(this.disabledSwipe)
      .onAnimationEnd((index) => {
        if (index !== this.active) {
          this.activeImage = {
            width: 0,
            height: 0,
            scale: 1,
            offsetX: 0,
            offsetY: 0,
            offsetStartX: 0,
            offsetStartY: 0,
            dragOffsetX: 0,
            dragOffsetY: 0,
          }
        }
        this.active = index
        this.disabledSwipe = this.activeImage.scale > 1
        this.activeImage.width = this.imageListSize[index].width
        this.activeImage.height = this.imageListSize[index].height
      })
展开全部
1楼回复于2026-04-02 11:25 未知归属地
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。