基于HarmonyOS ArkUI 3.0 框架,成功开发了相册
土司dgg 2021-12-02 10:54:15 发布3441 浏览 20 点赞 25 收藏
HarmonyOS ArkUI 3.0 正式到来,今天就带大家感受一下HarmonyOS ArkUI 3.0 框架开发的相册功能,功能很简单,主要是图片放大和图片左右滑动事件。
实现效果

项目开发
创建新的应用
选择一个空的模板

主要是选择开发语言为ets

代码实现
hml结构:渲染函数
build() {
// 超出屏幕,可滚动
Scroll() {
// 图片列表
Flex({ wrap: FlexWrap.Wrap, justifyContent: FlexAlign.Center, alignContent: FlexAlign.Center }) {
ForEach(this.imageList, (item, index) => {
// 循环图片
Image(item.url)
.width(item.width)
.height(item.height)
.opacity(item.opacity)
.onClick(() => {
// 保存点击的id
if (this.currentId === 0) {
this.currentId = item.id
this.currentIndex = this.currentId - 1
this.isBig = true
} else {
this.currentId = 0
this.currentIndex = -2
this.isBig = false
}
this.changeList()
})
// 滑动事件
.gesture(PanGesture({ direction: PanDirection.Horizontal })
.onActionStart((e: GestureEvent) => { // 左右滑动开始
// 保存开始的坐标
if (!this.isBig) return false
this.startX = e.offsetX
})
.onActionEnd((e: GestureEvent) => { // 左右滑动结束
// 计算左滑还是右滑,初始化开始坐标
if (!this.isBig) return false
this.isSlideL = e.offsetX - this.startX > 0 ? false : true
this.startX = 0
this.showAnimate()
})
)
.border({ width: 2, radius: 2, color: '#fff' })
})
}.width('100%')
}
}
- 数据处理
changeList() {
this.imageList = this.imageList.map((item) => {
const newI = item
if (item.id === this.currentId && this.isBig) {
newI.width = '100%'
newI.height = '100%'
newI.opacity = 1
} else if (item.id !== this.currentId && this.isBig) {
newI.width = '0%'
newI.height = 0
newI.opacity = 0
} else {
newI.width = '50%'
newI.height = 200
newI.opacity = 1
}
return newI
})
}
// 左右滑动,改变图片的透明度
showAnimate() {
// 计算下一个图片的index序列
let nextIndex = this.currentIndex
if (!this.isSlideL) {
nextIndex -= 1
} else {
nextIndex += 1
}
nextIndex = nextIndex === this.imageList.length ? 0 : (nextIndex === -1 ? (this.imageList.length - 1) : nextIndex)
// 将下一个图片序列赋值给当前的序列
this.currentIndex = nextIndex
this.imageList = this.imageList.map((item, index) => {
const newI = item
if (nextIndex === index) {
newI.opacity = 1
newI.width = '100%'
newI.height = '100%'
} else {
newI.opacity = 0
newI.width = '0%'
newI.height = '0%'
}
return newI
})
}ndefined
方舟开发框架声明式编程很符合接下来前端的趋势,希望框架能越来越完善。
©本站发布的所有内容,包括但不限于文字、图片、音频、视频、图表、标志、标识、广告、商标、商号、域名、软件、程序等,除特别标明外,均来源于网络或用户投稿,版权归原作者或原出处所有。我们致力于保护原作者版权,若涉及版权问题,请及时联系我们进行处理。
分类
其它
标签
HarmonyOS
开发
其他
相关推荐
微信鸿蒙版 App 扫码登录手表端要求公布,手机系统需升级至 HarmonyOS 6.0.0.130 及以上版本
1361
02026 HarmonyOS Connect伙伴峰会上海站圆满结束
1656
0三屏同开盯盘有多爽?鸿蒙版同花顺+折叠屏,用了就回不去!
一杯咖啡两千行
3000
098寸国产化户外广告屏一体机鸿蒙HarmonyOS系统校园
阿杰的阳光笔记
2801
0
土司dgg
我还没有写个人简介......
56
帖子
0
提问
201
粉丝
最新发布
鸿蒙生态创新开放能力论坛:全生命周期一站式服务,给创意“减负”,为成功“提速”
2025-12-02 11:48:16 发布华为鸿蒙二合一平板电脑正式发布,开启高效融合新场景
2025-11-26 16:25:39 发布热门推荐