HarmonyOS开发者版主1|
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 未知归属地
写回答
所见即所得编辑器, editor1
工具栏 撤消 快捷键 Ctrl+Z 重做 快捷键 Ctrl+Y 格式刷 快捷键 Ctrl+Shift+C 清除格式 加粗 快捷键 Ctrl+B 倾斜 快捷键 Ctrl+I 下划线 快捷键 Ctrl+U 删除线格式调整字号 文本颜色 背景颜色 增加缩进量 减少缩进量 左对齐 居中 右对齐 两端对齐段落间距行间距 项目列表 编号列表 插入水平线 块引用 插入/编辑超链接 快捷键 Ctrl+K 取消超链接 图片 表格 上传附件 导入Word 插入代码 MD编辑器 全屏模式
0/50000
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要提问题
相关推荐
换一批
1
HarmonyOS 手势响应过程中弹出图层,如何继续在新的图层上进行响应 (API12+)
2
HarmonyOS 手势响应过程中弹出图层,如何继续在新的图层上进行响应(API12+)
3
HarmonyOS 手势按下时,新界面出现,有什么办法能在新出现的界面中继续触发手势操作(API12+)
4
HarmonyOS 滑动手势会触发点击手势 (API12+)
5
HarmonyOS 如何实现手指捏放放大缩小图片和滑动移动图片?(API12+)
6
HarmonyOS 如何实现手势缩放图片?(API12+)
7
HarmonyOS 实现单个图片的缩放功能,放大后可以自由拖动查看细节 (API12+)
8
HarmonyOS 最小化再最大化,图片会抖动 (API12+)
了解社区公约,与您携手共创和谐专业的开发者社区。




































苏公安网备 32011402010933号